how to clamp( set max value) the velocity of a 2d rigidbody?
I'm making a typical block breaker game aka arkanoid, but I'm facing a problem with the ball getting a very big velocity as the game goes on, which I believe is due to:
1- the bounciness of the physics material which is set to 1
2- the force add to the ball through the paddle
to the point where it just penetrates through my boarders which are static 2d box collider. I managed to over come the problem by always checking the position of the ball and in case it is out of the view restart it's position and velocity, so in way this works but I want to prevent this from happening in the first place by setting limits to the balls velocity.
Answer by Foulcloud · Aug 25, 2015 at 04:46 PM
Try adding this into the code of your game object.
float maxVelocity = 10;
Void FixedUpdate()
{
rigidbody2D.velocity = Vector2.ClampMagnitude(rigidbody2D.velocity.magnitude, maxVelocity);
}
You could also check rigidbody2D.velocity.magnitude before adding the paddle force. If it is bigger than the maximum value, do not add the force.
Awesome thank you! By the way, I think it should be
rigidbody2D.velocity = Vector2.Clamp$$anonymous$$agnitude(rigidbody2D.velocity, maxVelocity);
The 1st parameter should be a Vector2
Your answer
Follow this Question
Related Questions
Trying to add force to an object... is this done correctly? 0 Answers
Rigidbody rotate velocity 0 Answers
MovePosition/Velocity not following a direction 0 Answers
Climbing code problem (2d physic). 0 Answers
Creating a Trampoline type object? 1 Answer