Object flipping on y and z axis when rotated on x axis

I’m making a simple hoverboard controller, but when trying to rotate the character on the x axis further than 90 degrees(pointing straight up) it starts flipping on the y and z axis
Any help would be great, thanks.

float h_input = Input.GetAxis(HorizontalControlsAxis);
float v_input = Input.GetAxis(VerticalControlsAxis);

leanAmount = Mathf.Clamp(leanAmount + -h_input * leanSpeed * Time.deltaTime, -maxLean, maxLean);

leanAmount = Mathf.Lerp(leanAmount, 0, Mathf.Abs(leanAmount) / maxLean);

Quaternion targetLeanRotation = Quaternion.Euler(transform.eulerAngles.x + v_input * vRotSpeed, transform.eulerAngles.y + h_input * rotSpeed, leanAmount);
transform.rotation = targetLeanRotation;

I’ve also tried transform.rotate on the player, but the same problem still occurs.

  • The reason is you have 4 quadrants 1) 0-90 Sine, Cosine, tangent are positive 2) 90-180 Sine Positive 3) 180-270 Cosine Positive 4) 270-360 Tan Positive. So when you go above 90 degrees the direction flips from positive and negative as well as the angle. The simple solution is to test angle and then use different formula for targetLeanRotation.

    – 

Leave a Comment