- Home /
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";
}
}
Answer by Ogreboy · Oct 29, 2021 at 04:14 PM
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. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613044746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Character Transform shifting after colliding with objects 0 Answers
Convert current rigidbody velocity to transform.forward 0 Answers
With power and angle, what is the best way to create a parabolic arc? 1 Answer
Move player in direction it's facing 1 Answer
My gameobject only moves to the right once and then stops, why? 3 Answers