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

Rotate Plane Using UI Slider

Discussion in 'Scripting' started by ConorArup, Feb 19, 2020.

  1. ConorArup

    ConorArup

    Joined:
    Feb 15, 2018
    Posts:
    17
    I'm trying to rotate a plane around the X, Y, and Z axis using individual UI sliders. Right now though, I think I've encountered Gimbal Lock on the X axis as the plane flips out when rotated between 90 - 270.

    I think it's because I'm using both Quaternions and Euler Angles, though I'm not sure how else to implement this using my current implementation. Can anyone help?

    Code (CSharp):
    1.  
    2.     public void XRotationSlider(float value)
    3.     {
    4.         UpdatePlaneRotation(Axis.X, value);
    5.     }
    6.  

    Code (CSharp):
    1.  
    2.     private void UpdatePlaneRotation(Axis axis, float value)
    3.     {
    4.         Vector3 newEulerAngle = planeUpdater.planeObject.rotation.eulerAngles;
    5.  
    6.         // Update the only value that was changed
    7.         switch (axis)
    8.         {
    9.             case Axis.X:
    10.                 newEulerAngle.x = value;
    11.                 break;
    12.             case Axis.Y:
    13.                 newEulerAngle.y = value;
    14.                 break;
    15.             case Axis.Z:
    16.                 newEulerAngle.z = value;
    17.                 break;
    18.         }
    19.  
    20.         planeUpdater.planeObject.rotation = Quaternion.Euler(newEulerAngle);
    21.     }
    22.  
     
    Last edited: Feb 19, 2020
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Here's a fairly exhaustive discussion with some good solutions
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    A quaternion can be represented by different combinations of eulers, and it's probably giving you one of those different combinations when you're close to gimbal lock.

    Try keeping your own vector rotation instead of using transform.eulerAngles each time.
    ex.

    Code (CSharp):
    1.  
    2.     Vector3 planeRotation = Vector3.zero; //initial zero rotation
    3.     private void UpdatePlaneRotation(Axis axis, float value)
    4.     {
    5.         // Update the only value that was changed
    6.         switch (axis)
    7.         {
    8.             case Axis.X:
    9.                 planeRotation .x = value;
    10.                 break;
    11.             case Axis.Y:
    12.                 planeRotation .y = value;
    13.                 break;
    14.             case Axis.Z:
    15.                 planeRotation .z = value;
    16.                 break;
    17.         }
    18.  
    19.         planeUpdater.planeObject.rotation = Quaternion.Euler(planeRotation );
    20.     }
    21.  
     
  4. ConorArup

    ConorArup

    Joined:
    Feb 15, 2018
    Posts:
    17
    It worked! Well, that was simpler than I thought it would be. In hindsight, if only one value is being changed at any one time, it didn't make sense for me to keep creating a new Vector3 and reading the object's rotation at that time.

    If you can, could you explain why reading the object's eulerAngles each time the value was changed caused this behaviour?