Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by wyatts · Oct 20, 2017 at 12:47 AM · rotationquaternionaxisrotation axisdisplacement

How would I interpret angular displacement from target rotation to max out at 180°?

I have two rotations, the objects current rotation, and the target objects rotation. I would like to create a single float that increases as the two rotations differ maxing out at 180° (facing opposite directions). I spent a long time looking into options and found "ToAngleAxis". This lets me grab a single number for the rotations and use that to find the difference.

Bear with me as this gets a little confusing - but the problem is that when the object is facing the opposite direction the displacement is 180° - but if it continues to rotate towards the target rotation it goes all the way to 360° instead of back down to zero.

Anyone have any ideas on how to get the displacement to max out at 180° and go back down to zero as it turns in a full circle?

Here's my code that will work with two cubes if you want to test out my problem:

     public Transform target;
     float curAngle;
     Vector3 curAxis;
     float targAngle;
     Vector3 targAxis;
 
     void Update () {
         transform.rotation.ToAngleAxis(out curAngle, out curAxis);
         target.rotation.ToAngleAxis(out targAngle, out targAxis);
         double displacement = 1-(curAngle-targAngle);
         Debug.Log(displacement);
     }

Aren't rotations FUN?!??!?

In case anyone is wondering WHY I'm trying to do this -- I'd love to sync the pitch of an audio source to how closely it's looking towards the player.

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
0
Best Answer

Answer by wyatts · Oct 21, 2017 at 12:19 AM

Thanks Harinezumi! Wow I can't believe I overlooked this. Such a simple tool!!! Definitely simplifies my approach, yet it returned the same result (it goes to 360 instead of maxing out at 180). I actually found a nice bit of code that just maths the numbers over 180 back down :)

     public Transform target;
     public float angleOut;
 
     void Update () {
         var displacement = Quaternion.Angle(target.rotation, transform.rotation);
         angleOut = ClampAngle(displacement, 0, 180);
         Debug.Log(angleOut);
     }
 
     float ClampAngle(float angle, float from, float to) {
         if(angle >180) angle = 360 - angle;
         angle = Mathf.Clamp(angle, from, to);
         if(angle <0) angle = 360 + angle;
         return angle;
     }
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
1

Answer by Harinezumi · Oct 20, 2017 at 07:56 AM

Hey @wyatts! I'm not sure this is what you want, but Quaternion.Angle() gives you the angle between 2 rotations, clamped to a value between 0 and 180, so if you divide it by 180 you'll get a float between [0, 1], maxing out at 180.

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 pako · Oct 20, 2017 at 07:27 AM

Maybe you can use the vector dot product to achieve what you want. Vector3.Dot returns a float between 1 and -1 for normalized vectors. -1 meaning they are pointing in opposite directions, i.e 180deg apart:

https://docs.unity3d.com/ScriptReference/Vector3.Dot.html

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 wyatts · Oct 21, 2017 at 12:25 AM 0
Share

Thanks for this! It could work, but the Quaternion.Angle() approach was a bit more direct. Cheers!

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

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

Related Questions

90 Degree stopping rotation on y-axis issue 0 Answers

Unity Simulate Local Rotation 0 Answers

Creating a multiple part turret what locks onto certain axis. 4 Answers

please help to rotate a cube around it's own axis 1 Answer

"Free" rotation about a sphere 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