How to measure forward velocity - InverseTransformDirection not working ?!

I’m making a simple car driving game.

I’m trying to display forward velocity on screen.

…But both rigidbody.velocity.z and transform.InverseTransformDirection(rigidbody.velocity).z return the same value. They both go negative when I turn the car around.

What am I doing wrong?

Thanks in advance!

public class UIManager : MonoBehaviour
{
    public GameObject car;

    Rigidbody carRb;

    public Text stats;
    public Text stats1;

    private void Start()
    {
        carRb = car.GetComponent<Rigidbody>();
    }

    private void Update()
    {
        Vector3 worldVelocity = carRb.velocity; 
        Vector3 localVelocity = transform.InverseTransformDirection(carRb.velocity);
        stats.text = "World Velocity Z: " + Mathf.Round(worldVelocity.z * 36)/10 + " Km/h";
        stats1.text = "Local Velocity Z: " + Mathf.Round(localVelocity.z * 36)/10 + " Km/h";
    }
}

188062-velo.jpg

Figured out my problem.

transform.InverseTransformDirection (of course) refers to the transform of the object to which this script is attached (which in my case is not the car that I’m trying to measure the speed of).

When I put:

car.transform.InverseTransformDirection(carRb.velocity)

everything worked out fine. :slight_smile: