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
1
Question by BarkShark · Nov 06, 2011 at 06:46 PM · rotationquaternionvalue360localeulerangles

Convert Quaternion to Eulerangles

Hello, in my game I have to compare two y-rotation values of an object before and after an action.

Little bit of my code:

 Function Update()
 
 var firstRotation = pivotPoint.transform.localEulerAngles.y; // pivotPoint is the gameObject wich I need the y-rotation values off
 
 // after the action
 
 var secondRotation = pivotPoint.transform.localEulerAngles.y;
 
 rotationValue = firstRotation - secondRotation;

This works great except when the secondrotation is less or equal to 360. I've looked it up in the script refecence and it says : Don't increment Eulerangles, as they will fail when the angle exceeds 360 degrees. That explains the problem.

If I use the Quaternions I get 4 strange numbers. Is there any way I can convert these 4 numbers to the y-rotation value?

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 syclamoth · Nov 07, 2011 at 03:29 PM 0
Share

Look up Quaternions on the Wikipedia, it gives a detailed explanation of what those 4 numbers actually mean. Needless to say, they don't really translate that well to things you'd be used to. Quaternions are to imaginary numbers what imaginary numbers are to real numbers!

2 Replies

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

Answer by aldonaletto · Nov 06, 2011 at 11:02 PM

eulerAngles and localEulerAngles aren't real variables - they actually are calculated by Unity based on transform.rotation and transform.localRotation, using the method you're looking for. But there are several 3-axes combinations that result in the same quaternion, thus Unity chooses one of them when returning the eulerAngles: a 180 degrees rotation around Y may result in (180, 0, 180), for instance, thus the after-before angle difference may give very weird results. On the other hand, assigning angles to eulerAngles or localEulerAngles always works fine.
If you need to know the exact angles all the time, you could do the contrary: have your own eulerAngles variable, rotate them in degrees by math and assign them modulo 360 to transform.eulerAngles - something like this version of Rotate:

var myEuler: Vector3;

function Start(){ myEuler = transform.localEulerAngles; // start with the initial rotation }

function Rotate(ax: float, ay: float, az: float){ myEuler = myEuler + Vector3(ax,ay,az); // rotate angles // assign the new angles in modulo 360: transform.localEulerAngles = Vector3(myEuler.x%360,myEuler.y%360,myEuler.z%360); } This is a local Rotate; if you use transform.eulerAngles, the rotation is world referenced.
SIDENOTE: Any 3-axes rotation can be converted to one single rotation around a specific axis, and that's what a quaternion encodes in its mysterious four elements (x, y, z, w): the w component means cos(angle/2), and x, y and z are the axis direction multiplied by sin(angle/2). Unity has functions to convert a quaternion to its angle axis representation and vice versa, thus you don't need to make this calculation by hand.

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
2

Answer by sdmodiano · Nov 07, 2011 at 04:50 PM

This is an awkward aspect of Unity. The zero crossing condition for euler angles is a pain. As it stands trying to compare one objects rotation to another objects is way more code than it needs to be. Using mod 360 is helpful, but it does not help with the zero crossing issue. I thought a lot about this and an elegant approach did not come to me. If there is some code that handles general case elegantly, I would like to see it.

I coded around my issue by using a different control interface. It turned out to be way better for me this time, but the euler angle zero crossing issue is still a pain.

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 aldonaletto · Nov 07, 2011 at 10:21 PM 0
Share

3-axes rotation is redundant: there are several, maybe infinity combinations that lead to the same result. That's why converting a quaternion back to 3-axes (Euler angles) gives so many weird results - Unity has to use some internal criteria to choose one of them. When the rotation is done around one root axis only (X, Y or Z) you may (not without pain and $$anonymous$$rs) create some correction algorithm; if the rotation affects all 3 axes, however, forget about it: the euler angles show absolutely unpredictable values! The only way is to do the inverse thing, like suggested in my answer.

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

Quaternion.ToAngleAxis is Unprecise? 1 Answer

Complicated Rotation Issue 1 Answer

Unity Simulate Local Rotation 0 Answers

Fixing the rotation around a particular axis using Quaternions? 2 Answers

Setting an objects angle about a local axis without localEulerAngles? 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