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
0
Question by unin · Feb 25, 2015 at 11:03 PM · c#rotationgameobject

Mathf.MoveTowardsAngle is not working as predicted

I'm using 'Mathf.MoveTowardsAngle()' to rotate an object towards an angle(zero). Sometimes it works as expected and other times it sets the rotation to a value close to zero(but not zero). I'm assuming this is because I'm using eulerAngles but input on why this is happening would be much appreciated. Here's a snippet of my code:

  /*targetRotation = 0*/
              float angle = Mathf.MoveTowardsAngle(myTransform.eulerAngles.z, targetRotation, moveSpeed * Time.deltaTime);
              myTransform.eulerAngles = new Vector3(myTransform.eulerAngles.x, myTransform.eulerAngles.y, angle);
  
              if (targetRotation == myTransform.eulerAngles.z)
              {
                  //Do Something(In my code, it just changes a enum value)
              }

Also I should note that this formula is placed in update and the rgidbody2D.fixedAngle of the object is set to true before running this(I set it true in order to negate the effects of physics on its rotation when it's moving towards the angle).

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

1 Reply

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

Answer by gfoot · Feb 25, 2015 at 11:08 PM

That is expected, the translation from Euler angles to Quaternion and back is not lossless - rounding errors cause slight variation of the results, and also the Euler angles are normalized into a specific set of ranges which can lead to sharp changes in value (e.g. 360 => 0).

You can either add some leeway to your "if", or test like this instead:

 if (targetRotation == angle)
 {
     ...
 }

which should work for your case. It would not necessarily work, though, for Y axis rotations, nor for certain key angles about any axis, due to the sharp jumps in Euler angle values at those points.

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 unin · Feb 25, 2015 at 11:50 PM 0
Share

I tried changing the if statement as you suggested but it's displaying the same results as my current if statement; the enum didn't change. Is there a way to evaluate the Quaternion directly so that there are no variation in the results?

avatar image unin · Feb 26, 2015 at 01:03 AM 0
Share

Your answer is correct, it is not possible for me to achieve the level of precision I want using Euler angles. So I decided to use Quaternion.Lerp and I've been able to achieve the results I wanted so far. Thank you for your Input.

avatar image gfoot · Feb 26, 2015 at 01:33 AM 0
Share

Great! Two other things you could consider - if nothing else changes the orientation, you could store the Z rotation in a member variable and update it from there. It won't work so well if other things drive the orientation of your gameobject though.

The other is to consider using Quaternion.RotateTowards ins$$anonymous$$d of Lerp. It behaves a lot like $$anonymous$$oveTowards, and you can use Quaternion.Angle afterwards to see how much further there is to go.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Make object only rotate in 90 degree increments 1 Answer

CharacterController.Move Not Corresponding to gameobject.transform.rotation 1 Answer

Gameobject rotation using gyroscope 0 Answers

C# Rotate GameObjects Regardless of List Size 2 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