- Home /
How to get the speed of an object?
So I know that you can get the velocity from the rigidBody. And I tried this. The problem I'm having, is that when I collide with an object, blocking the player, the velocity doesn't come back as 0, instead it is as if the player never stopped moving. Same thing happen's in reverse, when something collides with the player, causing it to move, the rigidBody.velocity still returns as greater than zero. Same thing happens in reverse. So when an object collides with the player while the player is standing still, causing it to move, the velocity returns as 0. I'm guessing it is because I change the velocity parameter of the player using the on screen controls. So how can I make it so that the player's speed comes back as 0 when he collides with something, and also make it not come back as zero when something else pushes the player?
I tried using:
speed = (transform.position - lastPosition).magnitude / Time.deltaTime;
lastPosition = transform.position;
But if I put it in the Update, it mostly comes back as 0 regardless if the player is moving or not. In LateUpdate, it will come back as zero every other Update. And in the FixedUpdate, it will work correctly for like 2 seconds, but then it starts throwing 2's for no reason.
Try the physics materials? Something like sticky might do what you want.
You should not directly change the velocity either. Better use "AddForce" or something.
What are you looking for? SpeedPerSec, distance moved that frame maybe?
Using your routine there gives me 2 to 4 times the speed value compared to velocity.magnitude, even when I use rigidbody.position.
But for all intents and purposes it works except for being inaccurate on curves. rigidbody.velocity is probably more accurate as it was the only thing in the debug which actually showed a 0 when reversing my direction.
I don't experience the FixedUpdate problem. There's something up there.
You must remember that velocity is directional but speed isnt. Thats why velocity is a vector and speed is a scalar.
Taking this in to account I think its pretty slim you'll actually ever get a perfect 0 for speed on a collision unless it is literally going EXACTLY back the way it came; and then the chances of getting your Debug to show that 0 are pretty slim as the time that object is at 0 for is too small. Given frame ti$$anonymous$$g the physics engine may have decided that your speed is already greater than 0 by the point your next debug comes about.
Imagine a powerful explosion. If the projected objects are held at 0 for a whole frame how is that going to look? (Hmm maybe actually quite good :/)
You may have better luck checking for 0 crossing points on each individual axis, in my opinion.
Answer by LeonardNS · Jul 20, 2015 at 11:10 AM
You could try
speedPerSec = Vector3.Distance (oldPosition, transform.position) / Time.dealtTime;
speed = Vector3.Distance (oldPosition, transform.position);
oldPosition = transform.position;
but I don't see how it would really make a difference.
NOTE: I wrote both speed and speedPerSec, but you can just remove one of them and the other won't be affected.
Can you tell me what the oldPosition is. Is it a vector3? please provide the code.
it would have to be a vector3 or you could not assign one to it.
Answer by Highwalker · Jul 21, 2015 at 08:10 AM
Thanks torigas! Using AddForce was almost the right thing, but does greatly improve the way my player moves overall. It was still a bit odd, and I had to achieve my desired effect in a completely different way than I had previously tried. But after long hours, I finally achieved what I was aiming for. Thank you everyone for your help :)
Answer by Jozi-Satler · Feb 06, 2021 at 06:24 PM
This should work:
float speed; Vector3 oldPosition;
void FixedUpdate()
{
speed = Vector3.Distance(oldPosition, transform.position) * 100f;
oldPosition = transform.position;
Debug.Log("Speed: " + speed.ToString("F2"));
}
How can you use "speed.ToSring("F2")" to set to a variable for a speedometer?
I get different results with the Debug.Log output when viewing in full screen game mode or just a small window. How can I make this exact same value with all screen sizes and frame rate?