Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by askull100 · Dec 11, 2016 at 11:34 PM · rotationquaternionlerpgimbal-lock

Is it possible to rotate an object around its z-axis without changing its x-axis and y-axis?

Hi Unity Answers,

I'm currently working on a small game involving a rotating cube. The player clicks a button, which Lerps the cube's rotation on the z-axis by 90 degrees. This works fine, but if I click the other button (which rotates the cube around the x-axis), the cube will behave strangely. Sometimes it will rotate in the wrong direction, sometimes it will jitter. But I know that one of the key problems here is the z-axis rotating the x-axis during the Lerp sequence.

Here is the code I use to lerp:

 IEnumerator LerpRotation(float a_Start, float a_End, float a_Duration, string a_Axis)
 {
     float i = 0;
     float Rotation;

     //A stylistic choice meant to make the cube rotate faster until it finishes. 
     float IncreaseRate = 0.0002f;
     while (i < 1)
     {
         //a_Duration, in this scenario, is 2.0f
         i += Time.deltaTime * a_Duration * IncreaseRate;
         Rotation = Mathf.LerpAngle(a_Start, a_End, i);

         switch (a_Axis)
         {
             case "x":
                 WonderCube.transform.eulerAngles = new Vector3(
                     Rotation,
                     WonderCube.transform.eulerAngles.y,
                     WonderCube.transform.eulerAngles.z);
                 break;
             case "y":
                 WonderCube.transform.eulerAngles = new Vector3(
                     WonderCube.transform.eulerAngles.x,
                     Rotation,
                     WonderCube.transform.eulerAngles.z);
                 break;
             case "z":
                 WonderCube.transform.eulerAngles = new Vector3(
                     WonderCube.transform.eulerAngles.x,
                     WonderCube.transform.eulerAngles.y,
                     Rotation);
                 break;
         }

         if (IncreaseRate < 2.0f)
             IncreaseRate += 0.05f;

         yield return null;
     }
 }

Again, this seems to work fine for the z-axis, but as soon as the x-axis is involved, the cube starts misbehaving. I've done some research and found something called Gimbal Lock (found here: http://answers.unity3d.com/questions/573035/avoiding-gimbal-lock.html), which might be related. However, I'm unsure if this is the same thing. I'd just like to be able to rotate around the z-axis without adjusting the direction of my x-axis and y-axis.

I'm thinking a dummy that doesn't rotate might work, but RotateAround is (according to Unity) obsolete, and I'm not sure how to use a dummy's axis for rotation.

I'd really appreciate some help on what I can do to improve this rotation code. Thanks very much.

EDIT: I've continued researching the problem, but I haven't found any solutions. I've tried researching if I was rotating in local space, but i'm using eulerAngles which is in World Space.

I also recorded a gif of the issue. Sorry for the low quality: http://imgur.com/a/BbwS7

It's hard to see it, but the jitter I mentioned is demonstrated in that gif. When I click the right button twice, the Y rotation and the Z Rotation jump to 180, despite the fact that only the X Rotation should be changed here.

I'd also like to clarify, as I have a better grasp on the problem now: my issues are that the rotation of the cube is follow the body, which I don't want, and that the cube is acting strangely when I rotate it on the X-Axis (regardless of the orientation of the Z-Axis and Y-Axis, as seen in the gif).

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 Owen-Reynolds · Dec 12, 2016 at 02:59 AM 0
Share

Angles snapping by 180 is normal - its because you're not supposed to modify single eulerAngles that way. x is also local, not global. EulerAngles are applied y,x,z, so only y is global.

You can probably use transform.Rotate(xx, Space.World). But for something more versatile, Quaternions and their math is better. I've got something at taxesforcatses (in the move/rotate) section, or you can probably find quaternions for beginners somewhere.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dpoly · Dec 12, 2016 at 07:54 AM

The basic answer to any question involving gimbal lock is: use quaternions not Euler angles. They work for cameras, rotating/tumbling objects, whatever. Recommended.

In most cases the problem just goes away. Quaternion.RotateTowards() or QuaternionFromToRotation() is your friend.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate on Transform.localeulerAngels smoothly 0 Answers

How to smoothly rotate gameobject 20 degrees on key press? 0 Answers

fromtorotation inacurate Help fixing? 0 Answers

Rotation (Quaternion.Lerp) not working 0 Answers

Smooth Rotation Quaternion Lerp? 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