How do I keep the speed of the Rigidbody2D constant?

Hi, I’m trying to create a “pong” like game. I want to keep the speed of the ball constant although let the direction of the ball to change based on the collision.

Currently I apply a force at the start function. I’m thinking of a way to set the speed at the update function although I could not figure it out as it needs a vector2d so the direction cannot be preserved.

Any tips or suggestions would be highly appreciated.

In the update function, you can get the current orientation of the ball by getting its normalized velocity, and then multiply that vector by the desired speed to keep it constant. Something like this could work:

Vector2 unit = GetComponent<Rigidbody2D>().velocity.normalized;
GetComponent<Rigidbody2D>().velocity = unit * constantSpeed;

The code above is going to work for what you need to do, but since you probably don’t use gravity and don’t want the velocity to change over time, I don’t think it’s necessary to use the whole Unity physics system with forces that are meant to simulate complex interactions.

Hi when you are working with phisics both 2d and 3d I think you should use the function FixedUpdate multiply speed and multiply Time.deltaTime.