- Home /
Rigidbody velocity limiter
I have objects that bounce on the water surface. To limit the vertical velocity, I use this:
public class LimitVerticalVelocity : MonoBehaviour {
public float limit = 1F;
public void FixedUpdate() {
rigidbody.velocity = new Vector3 (
rigidbody.velocity.x,
Mathf.Clamp(rigidbody.velocity.y, -limit, limit),
rigidbody.velocity.z
);
}
}
which works just fine. However, if I decide to disable it, whenever I enable it again, the rigidbody freezes, unable to move. How can I fix this?
Let see what Script reference has to say about that.
"In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour. Don't set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical example where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity."
http://unity3d.com/support/documentation/ScriptReference/Rigidbody-velocity.html
Try splitting that into two operations:
Vector3 curVelocity = rigidbody.velocity;
// perform whatever changes you need to make
rigidbody.velocity = curVelocity;
Your answer
Follow this Question
Related Questions
Rigidbody and Time.deltaTime 2 Answers
When is Velocity calculated? 1 Answer
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Can't remove Rigidbody from transform in script? 8 Answers
Enable or Disable Rigid Body at Runtime? 9 Answers