0
votes

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 enter image description here

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 enter image description here

and since I have no idea why it won't work, here is my physics project settings enter image description here

1
Have you tried changing RigidBody.Drag to zero? - DavidG
@DavidG I put that in my inspector window, my code, and update method in code and it changed nothing - Pookie
How about the bounciness of the ball and whatever it is hitting? - DavidG
@DavidG yup both at max - Pookie
For future reference, please do not repost questions with minimal alteration. Instead, edit your original question to improve its clarity and quality, and specify why existing answers fail to meet your requirements. - Serlite

1 Answers

1
votes

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 :)