You want to detect direction of vectors by using the dot product.
if( Vector3.Dot(transform.forward, Vector3.forward) > 0.0f )
// dude is walking forward
Here's a pretty clear way to learn about dot product:
http://www.youtube.com/watch?v=KDHuWxy53uM&feature=player_embedded
**Update**
I misunderstood what you meant by "walking forward", I was thinking forward in relation to the world, you're just checking to see if they're walking forward or walking backwards in relation to their forward vector. When taking the Dot between two normalized vectors, the result will be the cosine of the angle between the vectors. (Ex. Vector3.Dot(Vector3.forward, Vector3.up) == 0.0f).
The cosine function gives us +1 at 0° (when the two vectors are the same) and a -1 at 180° (when the two vectors are facing opposite directions) and at -90° and 90° it'll give us 0.
Hopefully a little more complicated example will show the relationship between dot product and angle between vectors.
// You can use rigidbody.velocity or a local variable to keep track of current velocity
float forwardDotVelocity = Vector3.Dot(transform.forward, rigidbody.velocity.normalized);
if( forwardDotVelocity > Mathf.Cos(60.0f) )
// Facing forward
else if( forwardDotVelocity < Mathf.Cos(120.0f) )
// Facing sideways
else
// Facking backwards
↧