Get the value of vertical speed of an object with rigidbody gravity

Hi guys,

I’m starting with Unity, and I’m trying the engine for the moment. I followed two excellent tutorials for beginners, who definitely set me in the right position on how to start game dev…

I’m currently working on my first project with Unity, and already finding some problems. As far as I’ve understood, I can set any gameobject a rigidbody, and use attraction with it. That’s actually what I did, because gravity is in use in the project I’m working on. Great. This is what I’ve done so far.

Cube created. Set it as rigidbody, and activate gravity. In the script now, here is what I do : on the update function, when the cube reaches 0, deactivate gravity, and freeze position on the Y axis to 0. This works perfectly now…

But… I would like to know the speed of the object when it hits position 0 on the Y axis, store it to a variable to be reused by another gameobject.

I’ve searched the doc (that’s where I found how to deactivate the gravity with scripting), but was unable to find a way to know the speed value of a given object with gravity on it.

A little help on that one ?

Thanks a lot

Vampyre

Edit : I found the function by deduction, and after that in the doc, but I cannot access its value :

rigidbody.velocity = Vector3(0,10,0);

This is the code to modify the velocity of a rigidbody. I would like to store that value. I cannot create a var float, or int as this is vector3. And when I try to create a vector3, it tells me I cannot call getcomponent when declaring a variable…

It’s generally a good idea to post the code you’re having problems with so people don’t have to guess what you’re doing… :wink: By the sound of it, you tried to do this:

var savedVelocity : Vector3 = rigidbody.velocity;

The assignment needs to happen at the point you want to store the velocity, not at script start-up. Instead, just declare the variable but leave out the initialization, or initialize to a constant place-holder value like Vector3.zero:

var savedVelocity : Vector3 = Vector3.zero;

Then assign the actual velocity at the time you want to save it:

function Update() {
    ...
    if (if cube has reached 0) {
        savedVelocity = rigidbody.velocity; // save the velocity
        // turn off gravity, clamp y to 0, etc
    }
}

Yeap i did something like that… I will think about the code next time i comes with a problem . Thanks for answering anyway… Now that i see the code, i wonder why i didn t found it myself… Probably the bad habit of always wanting to declare var somewhere i guess… I will have a try tomorrow. Thas for the tip… And sorry for the typo, i m on the ipad…