Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by bushdiver · Mar 27, 2013 at 07:52 AM · animationvector3mecanim

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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)?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bushdiver · Mar 27, 2013 at 06:42 PM 0
Share

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?

avatar image robertbu · Mar 27, 2013 at 06:51 PM 0
Share

With a rotation of (0,0,0), forward is the side facing positive 'z'.

avatar image whebert · Mar 27, 2013 at 06:55 PM 2
Share

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.

avatar image whebert · Mar 27, 2013 at 07:10 PM 0
Share

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;
avatar image bushdiver · Mar 27, 2013 at 07:13 PM 0
Share

This has been a big help, thank you

avatar image
3

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bushdiver · Mar 27, 2013 at 07:14 PM 0
Share

this worked as well, thank you

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Set Animator Parameter based on move direction? 0 Answers

Set Unity3D AnimationClip Property Without Resetting the Animator 0 Answers

I can't setting at once ModelImporter.sourceAvatar in script. 1 Answer

More realistically interaction with soccer ball 0 Answers

How to add new animations to enemy NPCs through mecanim? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges