Search code examples
c#unity-game-enginevelocitygameobjectrigid-bodies

Velocity in Unity 2D How to get velocity of object without rigidbody


By using method transform.translate(Vector3.left * 5f * Time.Deltatime; does this change the velocity of a gameobject with a rigidbody ,because in my case it doesnt work.Is there a way i can move an object so the velocity changes .If not is there any way to measure velocity of an object without having a Rigidbody attached to it. Thanks.


Solution

  • You can measure the velocity vector like this:

    Vector3 pos, velocity;
    
    void Awake()
    {
        pos = transform.position;
    }
    
    void Update()
    {
        velocity = (transform.position - pos) / Time.deltaTime;
        pos = transform.position;
    }