x


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;


}
more ▼

asked Mar 10 '10 at 11:08 PM

Oudaiesty gravatar image

Oudaiesty
200 8 13 20

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Mar 11 '10 at 01:49 AM

Lipis gravatar image

Lipis
2.2k 10 22 48

Remember to zero out the angularVelocity too!

Mar 11 '10 at 10:18 AM duck ♦♦

Thank you @Duck! I edit the answer..

Mar 11 '10 at 11:40 AM Lipis

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. MonoBehaviours also have a rigidbody accessor - edited your answer to reflect it.

Mar 11 '10 at 02:11 PM AngryAnt ♦♦

@AngryAnt thanks.. I also added the Sleep() in the answer and hopefully you don't mind that I changed the inch to centimeters... :)

Mar 11 '10 at 02:51 PM Lipis
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x316

asked: Mar 10 '10 at 11:08 PM

Seen: 2949 times

Last Updated: Mar 10 '10 at 11:08 PM