Determine Character Facing Direction

I’m trying to determine which way the character is facing so I can pass this variable to mecanim for use with animation selection…

I can get a Vector3 direction with Transform.forward but I can’t make any sense of (0,0,0)…
If I choose one variable Transform.forward.x or z , I can’t figure on how to determine where that’s facing…

for example using Transform.forward.x: looking up is .7, and looking left is .7 the same… I would like to work with a -180 to 180 direction… I’m lost on this, please help

The transform.forward is the correct variable to use. This represent a vector depicting the direction in 3 dimensional space your character is facing (assuming your character is setup where forward is initially along the Z axis).

You should use the entire Vector3, not just one of it’s components (x,y,z). The individual components by themselves won’t tell you much, but the entire vector gives you the forward direction of your character.

You mention you can’t make sense of (0,0,0), is that the value you get when you access transform.forward? That is odd if so, the transform.forward of any GameObject should always be a unit vector, pointing in the director of that object’s local Z axis.

You also mention -180 to 180, do you need to convert your forward vector into a heading angle (rotation about the Y axis)?

You can use Mathf.Atan2() to solve this problem. Angles start on the right and run counter-clockwise. A bit of untested code:

var heading = Atan2(transform.right.z, transform.right.x) * Mathf.Rad2Deg;

I don’t remember what range is returned by Atan2(), you might have to normalize it to your -180 to 180 degrees.

Sorry for hijacking but none of my forum posts have any responses so this is the closest posts I’ve seen to my problems so here goes thank guys:
I have been using this to determine the z axis facing forward or back but when the player faces x or -x I doesn’t work. It is to get a UI Icon to face either right left up or down as to where the wind is blowing due to the direction of the player, the wind is always blowing in the same direction but the arrow changes due to the players facing position, most of the UI arrow facing is either left or right by 180 degrees, rarely is it (Rotate.right, 90) etc, most are Rotate.up, 0 or 180 or Rotate.forward, 0 or 90.

public bool  aligned = false;
 
if(Mathf.Abs(Vector3.Dot(player.forward, Vector3.forward)) == 1) aligned = true;{
if (aligned) { WindArrow.transform.Rotate(Vector3.up, 180f); }else { WindArrow.transform.Rotate(Vector3.up, 0f); }
//This works up to here as the player is facing in the z direction, but afterwards doesn't as it faces the x direcion.

 if (Mathf.Abs(Vector3.Dot(Player.transform.right, Vector3.right)) == 1) aligned = true;
               
                    if (aligned) { WindArrow.transform.Rotate(Vector3.right, 180); } else { WindArrow.transform.Rotate(Vector3.right, 0f); }
// thought with player.transform.right, Vector3.right)) ==1 it would work as it would be x facing?

public bool  aligned = false;
 
if(Mathf.Abs(Vector3.Dot(player.forward, Vector3.forward)) == 1) aligned = true;{
if (aligned) { WindArrow.transform.Rotate(Vector3.up, 180f); }else { WindArrow.transform.Rotate(Vector3.up, 0f); }


 if (Mathf.Abs(Vector3.Dot(Player.transform.right Vector3.right)) == 1) aligned = true;
               
                    if (aligned) { WindArrow.transform.Rotate(Vector3.right, 180); } else { WindArrow.transform.Rotate(Vector3.right, 0f); }
            }

The UI arrow on the WindArrow.transform.Rtate(Vector3.right, 180) just doesn’t work? Any ideas how to fet the code working for moving in the x axis as I did for rthe z axis? Thanks for your time much appreciated.