I have a ridigbody that I move with AddForce. Then I read the speed with:
That gives me the speed in meters per second. Splendid!
But now, I need to know if the speed (of a vessel) is forward or astern. The magnitude always returns a positive speed, but when astern, I need to read it as a negative speed.
I am sure that it is a very simple question but I can’t find the answer anywhere. Thanks in advance.
@XienDev is right.
The easiest way to get the velocity relative to your object is:
var localVel = transform.InverseTransformDirection(rigidbody.velocity);
now localVel.z is the local z axis velocity part which will be positive when moving forward and negative when moving backward. Furthermore you have localVel.x which is the sideward speed (positive == moves right, negative == moves left) and localVel.y which is the up / down speed (positive == up, negative == down).
rigidbody.velocity - it’s velocity vector in world space, magnitude - it’s length