Search Unity

Question Confusing bug

Discussion in 'Scripting' started by JustYourFriendlyNeighbourhoodSlug, Sep 4, 2021.

  1. JustYourFriendlyNeighbourhoodSlug

    JustYourFriendlyNeighbourhoodSlug

    Joined:
    Aug 24, 2020
    Posts:
    24
    In my game, the player is able to rotate their gravity in a corridor platformer, which in turn Lerps the player's rotation to the right rotation for upright. the way I'm doing this is by checking whether the z-axis (the only bit that isn't just set to the player's rotation based on a camera movement script) is the same as the target z-axis, and if it isn't, I'm lerping it so that it is.

    The issue, however, is that when I have changed the player's rotation once, it stops the player's y rotation from changing, thus stopping you from looking to the side entirely.

    All relevant code:
    Code (CSharp):
    1. Quaternion GravRotation;
    2.  
    3. void Update()
    4.     {
    5.         if(Input.GetKeyDown("q"))
    6.         {
    7.             if(GravityState < 4)
    8.             {
    9.                 GravityState++;
    10.             }
    11.             else
    12.             {
    13.                 GravityState = 1;
    14.             }
    15.             GravChange();
    16.         }
    17.         if(Input.GetKeyDown("e"))
    18.         {
    19.             if(GravityState > 1)
    20.             {
    21.                 GravityState--;
    22.             }
    23.             else
    24.             {
    25.                 GravityState = 4;
    26.             }
    27.             GravChange();
    28.         }
    29.     }
    30.  
    31.     void GravChange()
    32.     {
    33.         if(GravityState == 1)
    34.         {
    35.             Physics.gravity = new Vector3(0f, -12.5f, 0f);
    36.             GravRotation = Quaternion.Euler(PlayerTransform.rotation.x, PlayerTransform.rotation.y, 0);
    37.         }
    38.         if(GravityState == 2)
    39.         {
    40.             Physics.gravity = new Vector3(-12.5f, 0f, 0f);
    41.             GravRotation = Quaternion.Euler(PlayerTransform.rotation.x, PlayerTransform.rotation.y, -90);
    42.         }
    43.         if(GravityState == 3)
    44.         {
    45.             Physics.gravity = new Vector3(0f, 12.5f, 0f);
    46.             GravRotation = Quaternion.Euler(PlayerTransform.rotation.x, PlayerTransform.rotation.y, 180);
    47.         }
    48.         if(GravityState == 4)
    49.         {
    50.             Physics.gravity = new Vector3(12.5f, 0f, 0f);
    51.             GravRotation = Quaternion.Euler(PlayerTransform.rotation.x, PlayerTransform.rotation.y, 90);
    52.         }
    53.     }
    54.  
    55.  
    56.     void FixedUpdate()
    57.     {
    58.         if (PlayerTransform.rotation.z != GravRotation.z)
    59.         {
    60.             PlayerTransform.rotation = Quaternion.Lerp(PlayerTransform.rotation, GravRotation, 10f);
    61.         }
    62.      }
    All help appreciated, thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Pretty sure this is gimbal lock. To see what I mean:

    - put a cube in a blank scene
    - using the inspector, independently rotate it a little bit around x, y, z - works as expected
    - now set x rotation to 90
    - try rotate around y and z - note that z works identically to y rotation! You can no longer rotate z!

    In the context of your game, I'm not sure what the best solution would be, but there's a few possibilities. Check out some youtube tutorials on changing gravity controllers...
     
    Jamolbek1 and Yoreki like this.
  3. JustYourFriendlyNeighbourhoodSlug

    JustYourFriendlyNeighbourhoodSlug

    Joined:
    Aug 24, 2020
    Posts:
    24
    I'll be honest I didn't even know this was a thing, I'll look into some tutorials then, thanks!
     
    orionsyndrome and Kurt-Dekker like this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    please do, because this is very basic, and the primary motivator for the quaternions instead of Euler angles.