How to tell if an actor is moving backwards or forwards?

If you take the dot product of the normalized velocity vector and the forward vector, the result will be near positive 1 when moving forwards (relative actor rotation) and near negative 1 when moving backwards:

forwardvelodot.png

The float output at the end is what you’re after. Technically you could skip the normalization and then just check if the output is larger than zero (forward), or less than zero (backward), but you would probably want to allow for some noise in the results. For example, a result of 0.00004359758 or something probably means you’re not really moving noticeably at all rather than definitely forwards, even though it’s technically larger than zero.

7 Likes