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
3
Question by aliakbo · Nov 11, 2013 at 10:19 AM · rotationquaterniongimbal-lock

Avoiding Gimbal Lock

Hey,

I am stuck trying to rotate an object around the X axis. I have tried numerous approaches but I have not been able to rotate the object past 90 and 270 angle. I have converted all my code to quaternions but whatever I do, I cannot get it to work.

This is my current code, I would appreciate it if someone could point out where I am going wrong. Thanks

 void Update () 
     {
         //get inputs
         pitch = Input.GetAxis("Pitch") * (Time.deltaTime * pitchSpeed);
         yaw = Input.GetAxis("Yaw") * (Time.deltaTime * yawSpeed);
         roll = Input.GetAxis("Roll") * .5f;
 
         //implement throttle
         thrust += Input.GetAxis("Throtle") * (Time.deltaTime * throtleSpeed);
         thrust = Mathf.Clamp(thrust, 10000, 60000);
 
         //smoothen rotaions
         smoothPitch = Mathf.LerpAngle(smoothPitch, pitch, Time.deltaTime * pitchSmoothness);
         smoothYaw = Mathf.LerpAngle(smoothYaw, yaw, Time.deltaTime * yawSmoothness);
         smoothRoll = Mathf.LerpAngle(smoothRoll, roll , Time.deltaTime * yTurnSmoothness);
 
         //gameobject rotation
         myTransform.rotation *= Quaternion.Euler(-smoothPitch, smoothRoll + smoothYaw, 0);
         //Have also tried this-
         //myTransform.Rotate(-smoothPitch, smoothRoll + smoothYaw, 0);
 
         //add thrust
         myRigidbody.velocity = myTransform.forward * (Time.deltaTime * thrust);
 
     }
 

  
Comment
Add comment · Show 2
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 AlucardJay · Nov 12, 2013 at 11:22 PM 0
Share

suggestion removed

Check this link out : http://keithmaggio.wordpress.com/2011/07/01/unity-3d-code-snippet-flight-script/

avatar image Hoeloe · Nov 12, 2013 at 11:44 PM 0
Share

That won't help - you're still converting it to the Euler representation, which is what causes Gimbal Lock.

2 Replies

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

Answer by Hoeloe · Nov 12, 2013 at 11:43 PM

I think the issue is that you're directly modifying the Euler angles - removing any benefit you get from having them internally stored as Quaternions. When you get the eulerAngles vector, or use Quaternion.Euler, you are essentially taking the representation in 3-dimensions and converting it to a quaternion. Much the same as if you convert a Vector2 to a Vector3, you don't gain any information by doing this, and so you lose the benefits of the Quaternion.

Instead of modifying the Quaternion via the Euler angles, try using the Quaternion.RotateAround method. This allows you to rotate the Quaternion around a specified axis, without every converting it to the Euler representation (and thus avoid Gimbal Lock). If you call this 3 times, one for each spacial axis, you should get the effect you want.

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 aliakbo · Nov 15, 2013 at 03:44 PM 0
Share

Quaternion.RotateAround does not exist. I'm wondering if you meant Quaternion.AngleAxis or something else?

avatar image Hoeloe · Nov 15, 2013 at 03:54 PM 0
Share

Sorry, you're right - it's Transform.RotateAround, you don't even need to bother going into Quaternions, though you can also do it with Quaternion.AngleAxis on each axis and multiplying them together (quaternion multiplication applies each rotation in turn).

avatar image aliakbo · Nov 15, 2013 at 08:30 PM 0
Share

This was really helpful, Thanks!

avatar image
1

Answer by zombience · Nov 13, 2013 at 12:25 AM

try Quaternion.AngleAxis

     public int xRot = 0;
 
     void Update () 
     {
         transform.rotation = Quaternion.AngleAxis(xRot, transform.right);
         xRot++;
         xRot = (int)Mathf.Repeat(xRot, 360);
     }
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

20 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 avatar image

Related Questions

Difference in rotation without gimbal lock 0 Answers

Clarifying the ambiguities of rotation in Unity 1 Answer

Limit local rotation around x, y, and z axes? 0 Answers

Rotation of child object is different in play mode 0 Answers

Yaw Pitch Roll order? 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