Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by hannacho700 · Oct 01, 2018 at 01:36 AM · angleyaw

How to get Yaw? or How to get Angle between two vectors over 180?

I wrote the code below to get Yaw. However, this code only gets -180 ~ 180 Yaw value. Let's try to illustrate what I want. I want the value to be 720 if the object rotate twice in the same direction.

 public float GetYaw()
 {
     var down = -transform.up;
     down.y = 0;
     Yaw = Vector3.Angle(-Vector3.right, down) * Mathf.Sign(transform.up.z);
 
     return Yaw;
  }
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

2 Replies

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

Answer by JVene · Oct 01, 2018 at 02:01 AM

Let's be clear about one point in your question. There is no information that comes from vectors which can tell you how many times a rotation has been performed. If an object rotated 765 degrees clockwise in the xz plane, the only information you'll get back from the resulting information in a Transform will be that the object rotated 45 degrees clockwise, because that is all the information that remains.


Vector3.Angle returns the angle between two vectors, without regard to any axis. If you define yaw as a rotation in the xz plane, there is no relevance to down or up. I would infer that a relationship to the world 'negative right' direction and the transform's 'down' direction is more that of a pitch than a yaw, but Vector3.Angle is going to return the angle between 'negative right' and wherever 'down' is pointing, which won't be relative to an axis. Imagine pointing your left hand straight out left (that's what negative right means, no?), then point your right hand ahead of you, but down from straight out 45 degrees. Now, in yaw (the xz plane), your arms are 90 degrees apart, but the angle between your hands will be somewhere less than 90 but greater than 45 degrees, in a plane at an angle not aligned with a reference to a yaw.


You can get the rotation of an object (in the range of -180 to 180 degrees) using Mathf.Atan2, providing the coordinates you get from transform.forward ( x and z, ignoring y ). The return value is in radians which you can convert by multiplying the return by Mathf.Rad2Deg. Note, however, that the standard orientation may require adjustment to your expectations, because in math zero degrees (or zero radians) is on the X axis (to the right), and positive rotation is counterclockwise. Once you adjust that for your expectation, you'll get the angle in the xz plane the object is facing.

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
avatar image
0

Answer by hannacho700 · Oct 01, 2018 at 02:47 AM

I omited some explanation. In my case, transform.up points his backside. So down means forward. then I used "down.y =0" to bring vectors to xz plane. So I can already get yaw based on left(-Vector3.right) in xz plane. but It's range is -180~180. so I want to get more big size of range.

Comment
Add comment · Show 3 · 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 hannacho700 · Oct 01, 2018 at 04:49 AM 0
Share

I think I solve my problem. So I share my idea.

 public float GetYaw()
 {
     var down = -transform.up;
     down.y = 0;
     Yaw = Vector3.Angle(-Vector3.right, down) * $$anonymous$$athf.Sign(transform.up.z);

     if (Yaw - preYaw < -300) round++;
     else if (Yaw - preYaw > 300) round--;
     
     roundYaw = round * 360 + Yaw;        
     preYaw = Yaw;
     return roundYaw;
  }
avatar image JVene · Oct 01, 2018 at 06:30 AM 0
Share

What do you mean by "get more big size of range". -180 to 180 is a full circle. There is no more to a rotation except spinning around and arriving at an angle inside that range. It could be interpreted as 0 to 360, which is basically the same thing.

avatar image hannacho700 JVene · Oct 02, 2018 at 12:54 AM 0
Share

If the object goes from -180 to 180, or from 180 to -180, the round variable is incremented or decremented, assu$$anonymous$$g that object turns around. And when you calculate the value, you can use round * 360 + yaw to get a value in the range greater than -180 to 180 , no matter how many turns the object is.

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

91 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 the rotation reference to an direction 0 Answers

How to change angle-roll object with mobile input? 0 Answers

How to make a touch joystick with PointerEventHandlers 0 Answers

Vector3.Angle outputs different results for the same values 0 Answers

add a force at an angle 3d 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