How do I get backwards velocity?

Hello,
In my game, a car is moved around be force. I calculate its speed using

transform.rigidbody.velocity.magnitude;

and show it to the user. When the object goes backwards, the velocity still stays in the positive values. That’s fine as long as I can tell that the object is moving backwards. But how can I do that? My only idea is using velocity.magnitude, but it returns a positive value anyway. Is there no way for my script to know if the object is moving backwards?

I’m not good with angles so I can’t do that. My idea would be to get the object’s facing direction and somehow calculate if the global x and z change and by what values. This looks like a very inefficient way to do things though.

Here’s a very similar problem as that of yours: Direction of rigidbody.velocity.magnitude

It has a working solution along with additional information regarding the Transform.InverseTransformDirectionSwitch().

velocity is a Vector3, which will always have a positive magnitude, what you are looking for is the dot product (difference in angle) between the velocity and the facing direction.

A dot product close to 1 indicated the direction of travel is forward-like, while a dot product of -1 indicates that the direction of travel is backward-like.

if (Vector3.Dot(transform.forward,Vector3.normalize(rigidbody.velocity))<0) iAmMovingBackward();