Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Problem with X-axis rotation

Discussion in 'Scripting' started by Hatemsla, Aug 11, 2023.

  1. Hatemsla

    Hatemsla

    Joined:
    Oct 17, 2021
    Posts:
    61
    I made 3 sliders to rotate the object along the axes, the slider values are from 0 to 360
    Rotation on the Y and Z axes works fine, but rotation on the X axis does not work correctly
    At some point, I get a 180 degree rotation on the Y and Z axes when rotating on the X. At the same time, the rotation methods are exactly the same and I don't have any special settings for the X-axis, it just doesn't rotate the way I expect
    Code (CSharp):
    1.         public void OnXRotationChanged(float value)
    2.         {
    3.             currentObject.Rotation = Quaternion.Euler(value, currentObject.Rotation.eulerAngles.y, currentObject.Rotation.eulerAngles.z);
    4.             Debug.Log(currentObject.Rotation.eulerAngles);
    5.         }
    6.  
    7.         public void OnYRotationChanged(float value)
    8.         {
    9.             currentObject.Rotation = Quaternion.Euler(currentObject.Rotation.eulerAngles.x, value, currentObject.Rotation.eulerAngles.z);
    10.         }
    11.        
    12.         public void OnZRotationChanged(float value)
    13.         {
    14.             currentObject.Rotation = Quaternion.Euler(currentObject.Rotation.eulerAngles.x, currentObject.Rotation.eulerAngles.y, value);
    15.         }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Is there some actual problem with the orientation or are you just confused by the displayed euler angles in the inspector? There is multiple solutions when converting between quaternions and euler angles.
     
  3. Hatemsla

    Hatemsla

    Joined:
    Oct 17, 2021
    Posts:
    61
    The bottom line is that it's not just the display in the inspector
    My object starts to flip suddenly while I'm moving the slider
    According to my logs, the following happens
    (x:57, y:0, z: 0)
    (x:58.3, y: 180, z: 180) (x:59.2, y:180, z: 180)
    (x: 60, y:0, z:0)
    This behavior simply flips the object as well changes its rotation on other axes, it looks like the object is sausage.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    You can't read back from .eulerAngles in the general sense.

    Track your own value in a float, adjust it, clamp it, use it to make new rotations.

    Here's why:

    All about Euler angles and rotations, by StarManta:

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html
     
    Yoreki likes this.
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    In one of my projects I had a complex setup of rotating bones within a blender model. Here is all the relevant snippets(I think):
    Code (CSharp):
    1. [Header("Right Leg")]
    2.     [Range(0, 230)] // 165 straight
    3.     public float rightLegRotX;
    4.     [Range(-60, 80)]
    5.     public float rightLegRotY;
    6.     [Range(-90, 20)] // inverse of left
    7.     public float rightLegRotZ;
    8.     Quaternion rightLegRot;
    9.  
    10.     void RightLegRotations()
    11.     {
    12.         SetInstantRotation(rightLeg, rightLegRot, rightLegRotX, rightLegRotY, rightLegRotZ);
    13.         SetInstantRotation(rightShin, rightShinRot, rightShinRotX, rightShinRotY, rightShinRotZ);
    14.         SetInstantRotation(rightFoot, rightFootRot, rightFootRotX, rightFootRotY, rightFootRotZ);
    15.         SetInstantRotation(rightToe, rightToeRot, rightToeRotX, rightToeRotY, rightToeRotZ);
    16.  
    17.         rightLegRot = rightLeg.transform.localRotation;
    18.         rightShinRot = rightShin.transform.localRotation;
    19.         rightFootRot = rightFoot.transform.localRotation;
    20.         rightToeRot = rightToe.transform.localRotation;
    21.     }
    22.  
    23.     void SetInstantRotation(GameObject bone, Quaternion modRot, float X, float Y, float Z)
    24.     {
    25.         Quaternion check = Quaternion.Euler(X, Y, Z);
    26.  
    27.         if (modRot != check)
    28.         {
    29.             //print($"{bone} set rotation");
    30.             bone.transform.localRotation = check;
    31.         }
    32.     }
    So not sure if anything in that can help spark a new idea, or give a fix for your situation. :)