- Home /
How do I zero out the velocity of an object?
The problem i'm having this time is that when I hit a group of objects and then reset their positions I can't Zero out their velocity so its like they are constantly being hit.
How can I zero out their velocity?
I have a script that was provided by -Duck but it needs something to fix that last issue.
Heres what he showed me:
var originalPosition : Vector3; var originalRotation : Quaternion;
@script AddComponentMenu("Misc/Location Reset") function Awake() { originalPosition = transform.position; originalRotation = transform.rotation; }
function ResetPosition() {
transform.position = originalPosition;
transform.rotation = originalRotation;
}
Answer by Lipis · Mar 11, 2010 at 01:49 AM
You can do that by setting the velocity
and angularVelocity
of the Rigidbody
to zero
:
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
And just to make absloutely sure that it won't move a centimeter you might want to call Sleep()
afterwards:
rigidbody.Sleep();
But according to Unity:
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.
The quote is right. A physical object would usually not stop dead suddenly, but that is all that is meant with that quote. Just to make absolutely sure you're not gonna move one inch further, you might want to rigidbody.Sleep (); right after zeroing the velocity. Ah and no reason to do transform.rigidbody. $$anonymous$$onoBehaviours also have a rigidbody accessor - edited your answer to reflect it.
@AngryAnt thanks.. I also added the Sleep() in the answer and hopefully you don't $$anonymous$$d that I changed the inch to centimeters... :)
Your answer
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Removing velocity relative to the contact normal 1 Answer
Mass affecting velocity 2 Answers
Target Range Situation 1 Answer
Local Velocity 1 Answer