Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by Apex · Sep 06, 2012 at 10:00 AM · localeulerangles

Problem with localEulerAngles (180 degrees error?)

Hello,

I just started learning Unity and I got a problem: I want to rotate a wheel around X axis (simulate movement) and around Y axis (simulate steering), using the following code:

 wheel.Rotate(collider.rpm * 6 * Time.deltaTime, 0, 0);  //rotate around X  
 wheel.localEulerAngles = new Vector3(wheel.localEulerAngles.x, collider.steerAngle, wheel.localEulerAngles.z); //rotate around Y

The problem is with wheel.localEulerAngles.z : sometimes it returns -1 or -2 and sometimes 180. - and this makes my wheel rotate on Z axis about 180 degrees constantly, something that I don't want. If I try to modify it's value (for example add only the values which are lower than 180) it "partially" fixes it but the wheel is not rotating correctly on the X axis.

Any simple solution is greatly appreciated.

*simple = not-very-complicated quaternions or no quaternions at all

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 Apex · Sep 06, 2012 at 12:16 PM 0
Share

@Fattie wheel.Rotate will rotate my wheel constantly, and I want to use this only for steering (rotating the wheels on Y axis only a bit, to allow the car to change it's direction).

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by aldonaletto · Sep 06, 2012 at 01:47 PM

The conversion quaternion->euler isn't reliable: there are several different xyz combinations corresponding to any quaternion, and many times the combination returned isn't the one we expect - specially when more than one axis is != zero.
Fortunately, the conversion euler->quaternion is reliable, thus we can use the following approach: get the initial Euler angles and rotate them mathematically, assigning the result to the actual Euler angles in Update (or other periodic function):

Vector3 euler;

void Start(){ euler = wheel.localEulerAngles; // save initial rotation }

void Update(){ // "rotate" wheel angle around X: euler.x = (euler.x + collider.rpm 6 Time.deltaTime) % 360; // set wheel Y angle: euler.y = collider.steerAngle; // update actual wheel localEulerAngles: wheel.localEulerAngles = euler; } NOTE: This works fine because Unity applies the Euler rotations in the order Z-X-Y, thus the angle about X will be set before the angle about Y. This trick would fail if the wheel rotated about Y and steered about X, for instance.

Comment
Add comment · Show 4 · 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 Kryptos · Sep 06, 2012 at 02:56 PM 0
Share

Fortunately, the conversion euler->quaternion is reliable

Where did you read that? I'm curious.

avatar image Hadden · Sep 06, 2012 at 03:10 PM 0
Share

Thank you! It works.

Sorry I couldn't reply earlier (question starter here) - this board has some problems with redirects but I'll mark this as answer once I'll be able to log in on my main account.

avatar image aldonaletto · Sep 06, 2012 at 05:16 PM 0
Share

@$$anonymous$$ryptos: the way Unity applies the rotations prevents gimbal lock. The rotations are applied to the object in the order Z-X-Y, but the axes are fixed and orthogonal. Gimbal lock may occur when you use an algorithm that somehow emulates real world gimbals: the gimbal axes are free to rotate, and in some situations two of them may become parallel (the famous gimbal lock). Unity probably uses some optimized form of Quaternion.AngleAxis(euler.y, Vector3.up) Quaternion.AngleAxis(euler.x, Vector3.right) Quaternion.AngleAxis(euler.z, Vector3.forward), which always produces a valid rotation (provided that the euler angles are valid, of course).

avatar image Sir_WaLeK · Sep 13, 2014 at 07:36 AM 0
Share

Omg u just made my day. Thnx a lot.

avatar image
1

Answer by Kryptos · Sep 06, 2012 at 11:47 AM

As a general rule, never modify localEulerAngles directly. Use quaternions instead.

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 jaskij · Sep 06, 2012 at 01:28 PM

IMO when it comes to coding, Euler angles are more complicated than quaternions, since there are so many friggin' exceptions you need to consider to avoid a gimbal lock.

Although I cannot give you a simple answer to your problem (there are too many unknows for now). Try putting Debug.Log(wheel.localEulerAngles.ToString()); there and look how the value changes (you need to open a Console window in Unity to see the output).

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

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

12 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

Related Questions

localeulerangles changes global angles? 1 Answer

Constrain XZ while setting local Y-rotation free. How? 1 Answer

Setting a local rotation via script results in a 180 degree difference in actual rotation 0 Answers

Raycast across angle? 3 Answers

localEulerAngles.x rotates as in world space 1 Answer


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