Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by cmonroy · Dec 04, 2014 at 12:10 PM · quaternion

Implementing a digital compass gives up to 180°

Hello.

I'm trying to implement a digital heading compass using the current rotation of my camera:

 void LateUpdate () 
 {
    currentRotation = this.transform.rotation;
    currentHeading = Quaternion.Angle(Quaternion.identity, currentRotation);
    compassHeading.text = currentHeading.ToString("000") + "°";
 }

It kind of works, since it gives the correct bearing until you reach 180° and after that point it starts to decrease the angle instead of keep adding it up until it reaches 359°. Since currentHeading returns a positive float regardless if I am turning East or West, there is probably one step I'm missing in my code.

Any help will be greatly appreciated.

Thanks.

Comment
Add comment · Show 1
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 cmonroy · Dec 04, 2014 at 02:39 PM 0
Share

[SOLVED thanks to Baste]

     void Awake()
     {
         northwardDirection = Quaternion.identity;
         westwardDirection = Quaternion.Euler(0, -90, 0) * northwardDirection;
         eastwarddirection = Quaternion.Euler(0, 90, 0) * northwardDirection;
     }
 
     void LateUpdate () 
     {
         currentRotation = this.transform.rotation;
         currentHeading = Quaternion.Angle(northwardDirection, currentRotation);
 
         float angleLeft = Quaternion.Angle(westwardDirection, currentRotation);
         float angleRight = Quaternion.Angle(eastwarddirection, currentRotation);
 
         if (angleLeft < angleRight)
         {
             currentHeading = 180 + (180 - currentHeading);
         }
 
         compassHeading.text = currentHeading.ToString("000") + "°";
     }

2 Replies

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

Answer by Baste · Dec 04, 2014 at 12:48 PM

Quaternion.Angle returns the angle between two rotations. Since it doesn't know which of the two possible angles you might want, it always picks the tightest one - which will give you a result between 0 and 180.

What you should do is to compare the rotation to two other directions - the identity turned 90 degrees to the right, and the identity turned 90 degrees to the left, and use which of those you're closest to to figure out where you're facing:

 Quaternion forward, rightward, leftward;

 void Start() {
     forward = Quaternion.identity;
     rightward = Quaternion.Euler(0, 90, 0) * forward;
     leftward = Quaternion.Euler(0, -90, 0) * forward;
 }

 void LateUpdate() {
     Quaternion currentRotation = this.transform.rotation;
     float currentHeading = Quaternion.Angle(forward, currentRotation);

     float angleRight = Quaternion.Angle(rightward, currentRotation);
     float angleLeft = Quaternion.Angle(leftward, currentRotation);

     if (angleLeft < angleRight)
         currentHeading = 180 + (180 - currentHeading);

     compassHeading.text = currentHeading.ToString("000") + "°");
 }

That works for me - putting it on a cube and replacing the compasHeading.text with a Debug.Log gives results between 0 and 359

Comment
Add comment · Show 2 · 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 Bunny83 · Dec 04, 2014 at 02:52 PM 1
Share

@cmonroy: Wait, you can't tick the answer as solution?
Tick the answer

If that's the case please post a comment so we can report it because everyone should be able to tick an answer on his own question.

avatar image cmonroy · Dec 04, 2014 at 03:40 PM 0
Share

Not me, Bunny83... I can't because there is not such icon for me.

avatar image
0

Answer by cmonroy · Dec 04, 2014 at 02:36 PM

Indeed it works. Thank you...

I would love to close this question but I still can't do it.

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

26 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

Related Questions

Rotate Gameobject by vertex shader with quaternion 0 Answers

Set (not offset) rotation on Vector3 axis? 0 Answers

Amplifying HMD rotation in VR 2 Answers

Damage Indicator not showing correct position of attack. 0 Answers

Setting the roll of a rotation 1 Answer


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