- Home /
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
Answer by whebert · Mar 27, 2013 at 06:38 PM
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)?
Yes I do need to convert my forward vector into a heading angle, how is this accomplished? Also, you noted (assu$$anonymous$$g your character is setup where forward is initially along the Z axis). I didn't knowingly setup a 'forward' direction, I just placed the player into the scene.. how is initial forward direction deter$$anonymous$$ed?
With a rotation of (0,0,0), forward is the side facing positive 'z'.
If you need to convert the forward vector into a "heading" angle (rotation about the Y axis), you could do something like the following.
// Get a copy of your forward vector
Vector3 forward = transform.forward;
// Zero out the y component of your forward vector to only get the direction in the X,Z plane
forward.y = 0;
float headingAngle = Quaternion.LookRotation(forward).eulerAngles.y;
In Unity, the "foward" of any GameObject is considered the Z axis (the blue axis in the Editor). If you want transform.forward to be in the same direction as your model, just insure whatever your model's front facing direction is along the Z (blue) axis. You should be able to rotate the model if needed in the Editor and perhaps then save your prefab in this default position.
Oh, yeah, if you need the headingAngle to be within 180 to -180, then do this after the above. The above method will get an angle between 0 and 360 degrees. The following will convert to 180 to -180.
if(headingAngle > 180f) headingAngle -= 360f;
Answer by robertbu · Mar 27, 2013 at 06:48 PM
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.
Answer by Paul_Hughes · Nov 30, 2021 at 06:01 PM
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.
Your answer
Follow this Question
Related Questions
Animation Events and Mecanim 0 Answers
What is the proper way to wait for an Animator Controller to update? 1 Answer
Mecanim Animations float til end of animation 1 Answer
Animation Gets Clipped When Speed Is Changed (Mecanim) 1 Answer
How can I change a mecanim animation by pressing a key? 1 Answer