简体   繁体   中英

Unity pong game ball physics slowing issue

In my game for pong, the ball is supposed to bounce and never become slower. However, the ball is steadily slowing down over time. I will put an image of the ball object and the scripts. Here is the ball properties on the left 在此处输入图片说明

Here is the ball script

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour 
{
    public float ballVelocity = 3000;

    Rigidbody rb;
    bool isPlay;
    int randInt;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        randInt = Random.Range(1,3);
    }

    void Update()
    {
        if (Input.GetMouseButton(0) && isPlay == false)
        {
            transform.parent = null;
            isPlay = true;
            rb.isKinematic = false;
            if (randInt == 1)
            {
                rb.AddForce(new Vector3(ballVelocity, ballVelocity, 0));
            }
            if (randInt == 2)
            {
                rb.AddForce(new Vector3(-ballVelocity, -ballVelocity, 0));
            }
        }
    }
}

and here is the bounce physics image 在此处输入图片说明

and since I have no idea why it won't work, here is my physics project settings 在此处输入图片说明

The reason why this is happening is because of the randomness being added. All it takes is it to hit harder in one direction one than the opposite direction. Eventually, idk after how long of a time, but it will finally settle at a velocity of 0. To fix this, you need to remove the drag coefficient if you haven't already. Next, you need to clear whatever the current velocity is from the ball. "rigidbody.velocity = Vector3.zero;" should do it for you. After that, you can either generate a new velocity directly using some maths that I do not know off of my head, or add a new force that is no longer dependent on the previous condition of the ball. I hope this helps, and if not, leave a comment and let's see if we can't find a better solution :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM