- Home /
Quaternion rotation flips axises when upside down
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/comments/2f81g6/quaternion_rotations_character_is_flipping_over/#ampf=undefined but they didn't offer me a solution, unless I implemented it wrong.
Your answer
Follow this Question
Related Questions
Reverse rotations problems... 2 Answers
Avoiding Gimbal Lock 1 Answer
Need help converting Euler-based rotation to Quaternion-based (gimbal lock). 1 Answer
unwanted rotation on the y and z axes 1 Answer
IMU Sensor and Quaternion 2 Answers