• 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
0
Question by LordNoodleFish · Mar 17 at 04:00 AM · rotationrotatebeginner

Object's rotation is messed up whenever it's x reaches 90

I am very new to Unity, and I am making a game where I need to be able to fly an airplane. I've been implementing the roll and yaw of the aircraft, and everything works, except when the x of the rotation reaches 90 - then the object is instantly rotated in all axes in a very strange way, and I can't tell what part of my code does this. If you have the time to help, it would probably be good to apply the following code to a cube or something to see what I mean. Many thanks for any help!

 public class PlayerMovement : MonoBehaviour
 {
     // Variables controlling roll of the aircraft; 
     private Vector3 rollRotation;
     [SerializeField] private float rollSpeed = 100;
 
     // Variables controlling yaw of the aircraft; 
     private Vector3 yawRotation; 
     [SerializeField] private float yawSpeed = 25; 
     
 
     private void FixedUpdate()
     {
         PlayerRoll(); 
         PlayerYaw(); 
     }
 
     private void PlayerYaw()
     {
         if (Input.GetKey(KeyCode.A))
         {
             yawRotation = Vector3.down; 
         }
         else if (Input.GetKey(KeyCode.D))
         {
             yawRotation = Vector3.up; 
         }
         else
         {
             yawRotation = Vector3.zero;
         }
 
         Vector3 finalYawRotation = yawRotation * yawSpeed * Time.deltaTime;
         Vector3 rot = transform.rotation.eulerAngles + finalYawRotation; 
         rot.y = ClampAngle(rot.y,-20f,20f); 
         transform.localRotation = Quaternion.Euler(rot);  
 
         float ClampAngle(float angle, float from, float to)
         {
             if (angle < 0f) angle = 360 + angle;
             if (angle > 180f) return Mathf.Max(angle, 360 + from);
             return Mathf.Min(angle, to);
         }
     }
     private void PlayerRoll()
     {
         // Checks which direction to apply roll, if any, and then applies it; 
         if (Input.GetKey(KeyCode.Q))
         {
             rollRotation = Vector3.left;
         }
         else if (Input.GetKey(KeyCode.E))
         {
             rollRotation = Vector3.right;
         }
         else
         {
             rollRotation = Vector3.zero;
         }
 
         Vector3 finalRollRotation = rollRotation * rollSpeed * Time.deltaTime; 
         Vector3 rot = transform.rotation.eulerAngles + finalRollRotation; 
         transform.localRotation = Quaternion.Euler(rot);
 
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Captain_Pineapple · Mar 17 at 10:26 AM

The issue you encounter here is a really really really common one for which i don't understand why Unity does not emphazise this issue in their tutorials.


Short version: Any rotation always has multiple representations as euler angles. (not counting that (180,0,0) would be the same as (180+360,0,0))

The only way to ensure consistent behaviour is by using quaternions as a quaternion is always unique for the representation of a rotation.

As unity always calculates rotations using quaternions and all euler angles are just used to make things readable for us humans, the result can thus jump from one representation to another when certain angle thresholds are reached.


That being said: there are hundreds of questions on this platform which solve the issue for clamped rotations. Please try to find a solution for this in there. If you are stuck with something specific, feel free to return and let us know.

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 Bunny83 · Mar 17 at 02:04 PM 0
Share

Right, in addition any combination of euler angles (and there are generally 6 different ways you can arrange the 3 angles hierarchically), it's always the middle axis that will cause a gimbal lock problem whenever it hits +- 90°. It's an inherent property of the way euler angles work. See this video for an explanation. Euler angles always suffer from gimbal lock. Quaternions do not have this issue because any relative rotation and orientation can be represented by a single rotation around a distinct axis. That's the beauty of quaternions. You can reach any orientation from any other orientation by a single rotation around a single 3d axis. That's what a quaternion essentially represents. Unfortunately quaternions are not that intuitive (especially since its math involved 4d complex numbers) but in most cases you don't have to care about what the numbers mean in a unit quaternion. Though if you're curious I can recommend the numberphile video on quaternions (the "extra bits" at the end may also help to better understand the non-comutativity).

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

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

Related Questions

How can I rotate an object to be perpendicular to a surface it hits (in 2D); 1 Answer

How to rotate an object according to the camera's view? 1 Answer

Split Rotation and Moving of a NavMeshAgent 1 Answer

Player Arm will not rotate when moving with Xbox Joystick 1 Answer

2d game,rotation problem...and need some help to prevent the camera from following my player when it rotates 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