Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Quaternion rotation flips axises when upside down

Discussion in 'Scripting' started by Sjiggle, Nov 19, 2018.

  1. Sjiggle

    Sjiggle

    Joined:
    Sep 12, 2012
    Posts:
    10
    I suspect this is called a gimbal lock but I can't really find how to solve it. I basically want my character to be able to walk around a ramp. Gravity is applied to the characters negative up vector.

    The player velocity is calculated and "stored" in a variable in world space. This should be translated / rotated towards the models local space to be able for it to move around, wether it's on a wall or ceiling.

    When the player walks against something it get's rotated towards the hitting plane (it's still an early piece of code) and should move further. This works until the character wants to be upside down. I suspect gimbal lock is happening at that point, where the axis is either turning around (you can't actually walk the ceiling in this case since it goes back and forth);

    var moveDirection = transform.TransformDirection(PlayerVelocity.normalized);
    moveDirection = rot * PlayerVelocity;
    transform.Translate(moveDirection * Time.deltaTime, Space.World);


    or it basically flips it's axis where the forward kind of turns around;

    var angle = Vector3.SignedAngle(Vector3.up, transform.up, transform.forward);
    Quaternion rot = Quaternion.AngleAxis(angle, transform.forward);
    moveDirection = rot * PlayerVelocity;
    transform.Translate(moveDirection * Time.deltaTime, Space.World);


    also not using quaternions and doing transform.translate in space.self gives the same problem as the first piece of code. What am I doing wrong here, can anyone advice me on what to do? I found sites like https://answers.unity.com/questions/284018/rigidbody-rotation-problem-when-upside-down.html and https://www.reddit.com/r/Unity3D/co...ns_character_is_flipping_over/#ampf=undefined but they didn't offer me a solution, unless I implemented it wrong.