Ball Speed

The ball speed is increasing or decreasing randomly.
I think that is because of the tweak we are using.
Please suggest a solution. I have tried many solutions from the community but nothing seems to work so far.

Hi,

Set the tweak value to a value very close to 0, for example, 0.001f. Did this fix it?


See also:

The ball is not changing speed but now it stucks in loop.

Hnet-image

Increase the tweak value slightly until the ball does not change its speed anymore but also does not get stuck.

If that does not work, set the tweak to 1 for testing purposes. Apply the tweak to the velocity, which you assign to a new Vector3 variable., then call Normalize on the Vector3 object. Multiply the object with your desired constant speed. Assign the object to velocity.

Now you should have a constant speed. Tweak the tweak value so the ball does not bounce off in funny angles.

I am unable to understand.
This is my code:

private void OnCollisionEnter2D(Collision2D collision)
    {
        Vector2 velocityTweak = new Vector2(Random.Range(0f, randomFactor), Random.Range(0f, randomFactor));
        if(hasStarted)
        {
            ballRigidBody.velocity += velocityTweak;
        }
    }

Please update this what according to what you are saying.
I have tried may solutions but problem is not solved and I am stucked and can’t proceed further in course. So please help.

Maybe this will work. I didn’t test it.

[SerializeField] float ballSpeed = 10f;

/// In your method:

Vector2 velocityTweak = new Vector2(Random.Range(0f, randomFactor), Random.Range(0f, randomFactor));

Vector3 newVelocity = ballRigidBody.velocity + velocityTweak;

newVelocity.Normalize();
newVelocity *= ballSpeed;

ballRigidBody.velocity = newVelocity;

If it works, you could apply the if-block where you launch the ball to apply the ballSpeed.

If you don’t understand the solution, don’t worry. It’s vector maths. If you are interested in this subject, you can find videos on Youtube. Alternatively, you could enrol in Ben’s and Gary’s maths course “Math For Video Games: The Fastest Way To Get Smarter At Math”.

1 Like

Ok I will try this.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms