# Keep a rigidbody velocity constant

I have a "board" with a ball (rigidbody) moving over it and colliding with obstacles.

I want its velocity magnitude to remain constant throughout time.

How do I achieve this?

What I've tried/achieved so far:

• My first guess was to use OnCollisionExit to set the velocity manually, but that lead to weird physics.
• I don't see how to use AddForce to do this since the previous movement would still be factored.
• To be clear, what i want is to set the velocity at all times to rigidbody.velocity.normalized x magnitude, where magnitude is a float I set previously. This would allow the physics engine to make all impact math to decide the direction of the ball after the collision, but my math would keep the velocity intact.
• I then decided to add UpdatedFixed and on every call set the velocity as stated above. It's working, but feels like a bad hack because I'm updating the velocity every frame and forsaking the physics engine.
• I just couldn't understand one point in your question... do you want the velocity to stay the same, or is it allowed to changed direction upon collision with other objects? – Ali1S232 Mar 9 '16 at 22:51
• It seems you are not updating your positions yourself, but have a physics engine involved, probably for collision detection. Does your physics engine allow you to specify the kind of reflection that occurs upon impact? You want an ideal collision at hard walls, where momentum is conserved. – mvw Mar 10 '16 at 11:49

I feel like you're on the write track with the 'bad hack'.

You're trying to do magical things with a component designed to approximate some real-world physics.

Inside FixedUpdate, Update or LateUpdate:

rigidbody.velocity = _constantSpeed * (rigidbody.velocity.normalized);


or some similar variation.

Don't need interaction with other physics objects? Then you don't need the RigidBody entirely; simulate motion with something like the above code, detect collisions and let the instigator (your ball) tell the other collider how much force should be applied. It's probably not necessary

• Hey, thanks. Yeah, I'm actually thinking about going back to non-physics engine movement, but I will still need the rigidbody to get collisions, right? – Bruno Brant Mar 12 '16 at 13:23
• @BrunoBrant Yes, you will need a minimum of a Collider and a Rigidbody to use MonoBehaviour methods like OnCollisoinEnter, OnTriggerEnter. Set the Rigidbody.IsKinematic to true in inspector or via code and it will not be moved by the physics engine. – Happy Apple Mar 13 '16 at 8:00