Search Unity

Stop Object From Moving When It Hits Max/Min Value (Gimbal Lock?)

Discussion in 'Scripting' started by XochiInteractive, Jul 7, 2021.

  1. XochiInteractive

    XochiInteractive

    Joined:
    May 21, 2018
    Posts:
    19
    I followed this tutorial (https://www.youtube.com/watch?v=537B1kJp9YQ) and I setup my camera extremely similar to it. An issue I noticed in this tutorial as well as in my game is that the game will start to rotate on the X when it hits the max/min rotation value on the Y.

    If that doesn't make sense here's an explanation; otherwise, skip to next paragraph. Let's say a fly can move anywhere in the room and the ceiling and floor are its max and min. If you move the fly up and it hits the ceiling, it should not longer move because you're only trying to move it up and not on the X or Z. However, when it hits its max, the ceiling, it will start to move on the X, left/right. It doesn't move left/right before that unless you tell it to.

    Is this Gimbal Lock? If so, has anyone used this tutorial and resolved the issue? When I research Gimbal Lock I haven't found out how to resolve my issue. I've seen people struggling with door or object orientation not rotating properly but couldn't find any issues matching mine.

    The code snippet is the code used to execute the Follow Target's movement which the camera is locked to and follows. I hope this all made sense.
    upload_2021-7-7_15-11-53.png
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It sounds a lot like Gimbal Lock, yeah. It basically boils down to, Euler angles are terrible. There are very narrow circumstances where Euler angles will not cause problems of this kind. I made a video about this subject that may help make sense of it. You'll want to avoid "reading out" the Euler angles of a transform like the plague - store and calculate your angles internally, then you can set .localEulerAngles without issue.
     
    XochiInteractive likes this.
  3. XochiInteractive

    XochiInteractive

    Joined:
    May 21, 2018
    Posts:
    19
    I watched your video and forgive me on my lack of knowledge on Quaternions. So from what I'm gathering from what you said I can set a .localEulerAngle without issue but the issues arise when reading a .localEulerAngle value correct? I've been messing around with different solutions and tried using the Mathf.Clamp instead of manually locking the angles like I did previously. However, in my current situation I can't move the followTarget gameobject at the moment. I have my input values being read on my PlayerController script which this script, PlayerMovement, grabs those values and reads them. I had my followTarget rotate in accordance to my mouse input, just when you move the mouse up/down rapidly it randomly rotates my followTarget gameobject maybe 180 degrees in a forced movement. upload_2021-7-8_20-12-16.png
     
  4. XochiInteractive

    XochiInteractive

    Joined:
    May 21, 2018
    Posts:
    19
    Okay so I reverted my script back to the original post and I know my issue is when I set var angles = followTarget.transform.localEulerAngles because I'm reading the value in that instance. I'm trying to find out how to get that value and edit it but am not completely sure how I can accomplish this task. Is Mathf.Atan2 the route I should seek and completely scrap what I currently have? Sorry for all the questions just new to experiencing Gimbal Lock.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Yep, you've got it. Use Atan2 so that you know reliably which axis you're reading the rotation of, without interference from the other axes.

    One thing I just noticed in your original post:
    Rotating on the X axis is actually up/down (like nodding your head), because it's rotating AROUND the X axis, not towards/away from it. Turning left/right (like shaking your head no) is around the Y axis, and cocking your head is around the Z axis.
     
    XochiInteractive likes this.
  6. XochiInteractive

    XochiInteractive

    Joined:
    May 21, 2018
    Posts:
    19
    Thanks for the info! I'm doing research into Atan2, never even heard of it until your video thanks for sharing! I'll post here the results after changing it around.