- Home /
Is there a way to detect which way an object is facing?
Hi guys,
Quick question- what is the most efficient way to detect which way an object is facing?
To provide an example, I have a character who can move forwards, up, or down (using the arrow keys). What I want to be able to do is:
a) Establish which way the character is currently facing. b) Once established to either rotate that character 180 degrees (to flip his direction) if the player tries to move 'backwards' (e.g. the opposite way he's currently facing) OR effectively do nothing if the character is just trying to move forwards (the way he's currently facing).
Once I can work out how to detect which way he's facing, I'll be able to add in my script to rotate him.
Thanks in advance for any suggestions.
Thanks, Sam
Answer by fafase · Aug 25, 2012 at 02:57 PM
Take the normal and use dot product of normal and vector to compare, the result will tell you which way it is facing
Vector3.Dot(object.normal, Vector3.forward);
0 -> perpendicular to the forward
<0 ->two vectors are opposite
0 ->two vectors are in the same direction
You can compare any two vectors.
Hi Fafase, thanks for the suggestion... I'll look into integrating this and let you know how well it works!