I'm working on a project in Unity 2D for learning purposes. It's a game of Ping Pong. I have its material so it travels faster every time it bounces. The only problem with its material is that its speed gets out of hand and glitches out. I want to find out a way to stop it.
For example, I would like to know how to keep the ball at a constant speed when it hits peak speed, example, 15f. It is a Rigidbody2d
collider ball.
As suggested in this post, you could control the velocity of the
your rigidbody using the Vector2.ClampMagnitude
method while tracking it in your OnFixedUpdate()
. I'm providing the code in the post I'm citing for your convenience. I edited the code I provided from the cited post to match the recent changes in the API:
float maxVelocity = 10;
void FixedUpdate()
{
rigidbody2D.velocity = Vector2.ClampMagnitude(rigidbody2D.velocity,
maxVelocity);
}