- Home /
BEST way to get velocity Without rigidbody?
I know you can manually get velocity with Vector3 velocity = (previous - transform.position)
but that's not really smooth. I've tried creating an array of the previous 5 transforms over frames and dividing by 5 (subtracting [0] from [4]) but I drop frames and it's inefficient. What's the best and most efficient way to get velocity?
Its worth noting that without a rigidbody the GameObject will only move as instructed by your code. So this question is equivalent to figuring out what velocity you told the GameObject to move at.
Answer by Owen-Reynolds · Nov 20, 2014 at 06:11 AM
Are you using Time.deltaTime
in your math? Or saving Time.time
for old frames?
Otherwise different length frames will throw things off. Ex: code moves 10 meters in a 0.1 second update, and correctly moves 12 meters in a 0.12 second update (which is no change in speed.)
Could at least print Time.deltaTime
to see if it varies (or do you move in FixedUpdate?)
Using 5 frames back is probably too much. That trick is purposely to make the estimate lag a little behind the real speed. To reduce "jitter," I think even the previous 2-3 frames should be fine.
Answer by meat5000 · Nov 20, 2014 at 01:33 AM
Velocity = Displacement / Time
Just like
Speed = Distance / Time
except Velocity is directional so we use displacement instead.
Your answer
Follow this Question
Related Questions
Is it a bad practice to change the timescale for the whole game? 1 Answer
Using Time.DeltaTime doesn't result in framerate independence 1 Answer
Use Velocity to determine future location of a character given a duration. 1 Answer
Is there any way to determine Unity's actual target frame rate? 1 Answer
Velocity powered rigidbody on a moving platform without parenting. 3 Answers