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 senabryer · Oct 11, 2015 at 06:49 PM · rotationeulerangleslocaleulerangles

localEulerAngles has trouble above 87.5 degrees

I've got a sun object that rotates around the world just fine, but when I try to make a time system that gauges the time by the sun's position in the sky, using transform.localEulerAngles.x (it rotates around a Game Object at world origin).

It all works perfectly until I get to 87.5 degrees above the horizon, in which it rounds it up to 90 (see: pic). Is this just a quirk of Eulers that they get weird above that number, or does this look like a bug?

bloob.png (235.4 kB)
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 meat5000 ♦ · Oct 11, 2015 at 06:49 PM 0
Share

Looks like some kind of error; Not a bug. Likely a coding issue.

1 Reply

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

Answer by Eno-Khaon · Oct 11, 2015 at 07:36 PM

In your screenshot, I notice that the Y and Z rotations of your sun are approximately 180 each. If you keep an eye on it longer, you might notice that your X-axis rotation ranges from 0-90, then goes back down to 0, resets to 360, goes to 270, then back up to 360/0 to 90, etc. This is a problem related to Gimbal Lock.

Look at it from this perspective: If you rotate an object by 180 degrees on all three of the X, Y, and Z axes, your object will look like it hasn't been rotated at all. This is because there are numerous ways of interpreting the same rotation.

When Unity constructs its rotations, all that really matters is the current final state; because there are an infinite number of ways to represent the same final orientation after a rotation, Unity is simply picking one that works first.

However, this doesn't factor in the use of Quaternion rotations, which can be given more explicit instructions. With Euler angles, you're essentially saying "rotate to 570 degrees, which is also known as 210 degrees, which is also known as -150 degrees" -- By contrast, using Quaternions, you would say "Perform 1.583333 revolutions around suchandsuch axis". The expected outcome is the same, but the actual results are very different.

So, what does this mean for you?

It means you should take a different approach to calculating the current rotation of the sun. What I would recommend is creating a function to calculate a signed angle between two vectors, and use that to determine the sun's angle instead.

 // C#
 
 // worldFwd is your baseline. What direction does the sun point at, say, noon?
 // localFwd is the current direction the sun is facing
 // localRight is a perpendicular vector to localFwd located on the rotational plane
 // In the case of your example, this would be the sun's -Y axis by default
 // (i.e. "up" would therefore be the axis rotated around in this case or, in your example, X axis)
 float SignedAngle(Vector3 worldFwd, Vector3 localFwd, Vector3 localRight)
 {
     float outAngle = Vector3.Angle(worldFwd, localFwd);
     if(Vector3.Dot(worldFwd, localRight) < 0)
     {
         outAngle *= -1;
     }
     return outAngle;
 }

Using this as-is, your return angle will range from -180 to 180 degrees relative to your "worldFwd" starting point, going counterclockwise.

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 senabryer · Oct 13, 2015 at 09:07 PM 0
Share

Using the angle looks to have done the trick. Though I did have to tweak it so that it uses position rather than rotation, since I still couldn't get around that "anything above 87.5 is rounded up to 90" bug. This works just fine so far; my only concern is that I need to start the sun exactly on the horizon to get an accurate rotation (unless I want to go in and actually jot down the position of the sun for every hour:$$anonymous$$ute combination I need)

Thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

localEulerAngles don't seem to work in an equasion 3 Answers

Bike stay upright based on surface normal 1 Answer

Complicated Rotation Issue 1 Answer

Why Local Rotation Faster than World Rotation (Degrees/Second) 1 Answer

Fixing the rotation around a particular axis using Quaternions? 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