- Home /
Determine the velocity of a game object.
What transform method in Unity would I use to determine the game object's velocity?
Thanks CSDG
Comment
Answer by hellcats · May 30, 2011 at 02:27 AM
If the object has a rigidbody attached then just use the rigidbody's velocity. If not, then you can save the transform's position on the previous frame, and subtract from the current one:
using UnityEngine;
public class Test : MonoBehaviour {
Vector3 _prevPosition;
void Update() {
Vector3 vel = (transform.position - _prevPosition) / Time.deltaTime;
_prevPosition = transform.position;
}
}
Is this used in $$anonymous$$onobehaviour Update()? I was wondering if used in FixedUpdate() maybe the Time.deltaTime can be removed!?
Thanks CSDG
If you put it in FixedUpdate then you'll need to use Time.fixedDeltaTime ins$$anonymous$$d (but Time.deltaTime is the same thing in FixedUpdate, so you could still use it, but it might be a little more clear to used fixedDeltaTime)
Your answer
![](https://koobas.hobune.stream/wayback/20220612092024im_/https://answers.unity.com/themes/thub/images/avi.jpg)