• 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
Question by zharik86 · Dec 30, 2013 at 06:13 PM · rotationquaternionangle

Quaternion and rotation angles

Good afternoon. I have the following expression:

  Quaternion myq = Quaternion.Euler(0, 0, zAn) * Quaternion.Euler(0, yAn, 0) * Quaternion(xAn, 0, 0);

Prompt how to solve the reverse problem. I have a value of a quaternion of myq, and it is necessary to find angles xAn, yAn, zAn(xAn=?, yAn=?, zAn=?). Maybe formula or function of Unity. Thanks in advance for the help.

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Benproductions1 · Dec 31, 2013 at 06:12 AM

Hello,

When using an API you are not used to, it's a good idea to read through the documentation of something before you use it. Then you wouldn't have to ask questions and waste your own time :)

If you take a look at the documentation for Quaternion here, you can see that .Euler makes a new quaternion rotation with euler-angles, and the field eulerAngles gets the euler-angles from a quaternion.

Note that there are multiple euler-angles for every Quaternion so it might not be what you expect :)

Hope this helps,
Benproductions1

Comment
zharik86

People who like this

1 Show 5 · 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 zharik86 · Dec 31, 2013 at 09:28 AM 0
Share

@Benproductions1 If you use Debug.log for myqD.eulerAngles, that you will see values not similar to xAn, yAn, zAn. Can do it the next bug of Unity4.1.2, but precisely I don't know.

avatar image Benproductions1 · Jan 01, 2014 at 01:42 AM 1
Share

@zharik86 "Note that there are multiple euler-angles for every Quaternion so it might not be what you expect". The values are completely correct and it's definitely not a bug with Unity (as @robertbu stated)

Take a look at this question about quaternion and euler-angle conversion

avatar image zharik86 · Jan 01, 2014 at 08:21 AM 0
Share

@Benproductions1 Thanks. I just started doing the function of conversion of a quaternion in Euler angles. As I noted, quaternions written are equal below:

  Quaternion myq = Quaternion.Euler(0, 0, zAn) * Quaternion.Euler(0, yAn, 0) * Quaternion(xAn, 0, 0);
  Quaternion myqD = Quaternion.Euler(xAn, yAn, zAn);

Thus, myq = myqD. But Euler angles of these quaternions are various, i.e. (myq.eulerAngles != myqD.eulerAngles). My function of converting becomes the decision as I think.

avatar image Benproductions1 · Jan 01, 2014 at 08:36 AM 0
Share

Actually, because of the way the conversion works in Unity, any equal Quaternions will always give the same EulerAngle representation. to be more accurate:

 Quaternion(x, y, z).eulerAngles != Vector3(x, y, z)
avatar image zharik86 · Jan 01, 2014 at 01:57 PM 0
Share

@Benproductions1 I tried to check how Unity calculates quaternions. I found conversion formulas (on wiki). From written above, myq quaternion I receive object orientation in space (being guided by three angles of xAn, yAn, zAn - physical orientation). If to be coordinated with formulas, it turns out that myq=myqD. Though in Unity it not so. Probably, there it is considered on another as y axes and z are traded places (that are this on wiki).

avatar image

Answer by robertbu · Dec 31, 2013 at 10:05 AM

As @Benproductions1 says, there are multiple euler angle representations for any given "physical" rotation. An eularAngles are derived from the Quaternion, so euler values are not saved. For example, run this code:

 transform.eulerAngles = Vector3(180,0,0);
 Debug.Log(transform.eulerAngles);

The value you will get back is (0,180,180)...the same physical rotation, but in a representation different then the one you set.

One solution is to preserve your own Vector3 and treat eulerAngles as write-only. For example:

 public var speed = 20.0;
 private var angles = Vector3.zero;
 
 function Update() {
     angles.z += Input.GetAxis("Horizontal") * speed * Time.deltaTime;
     angles.x += Input.GetAxis("Vertical") * speed * Time.deltaTime;
     transform.eulerAngles = angles;
 }

Note that changes are always made in 'angles' and 'eulerAngles' is then set (but never read). If I want the euler values, I can read them from 'angles'. I can always manipulate or normalize 'angles' for whatever my task. This technique does not work for every situation.

Comment
zharik86

People who like this

1 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 zharik86 · Dec 31, 2013 at 03:30 PM 0
Share

@robertbu That you wrote that remarkably is suitable for the majority of cases. But unfortunately not for me. Initially I have the initial quaternion (from a gyroscope). But I need to take from it angles as my platform rotates like a gyroscope, but with angles ((x, 0, y) from a gyroscope).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Bone rotation in LateUpdate goes back to normal every frame 2 Answers

object is not rotating from right direction 0 Answers

Smooth Y rotation based on X rotation (C#) 0 Answers

How to get angular difference? 2 Answers

Character doing 360s randomly 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