Model Jolts to weird 90 degree angle on Game start

No matter how I set it in scene view the model jumps to a verical position and just stays there.

I’ve checked and re-done the animation, swtiched out the model in Player Rig Empty, checked my code against Rick’s and it’s fundamentally the same (see code below)

Looking online all I can see is things about the “Gimbal Lock” from the Euler but how have I caused that?

Hopefully it’s something simple that I’ve missed, so can someone take a look?

Thanks

using UnityEngine;

public class PlayerControl : MonoBehaviour
{
  [SerializeField]float offsetSpeed = 8f;
  [SerializeField] float xRange = 8f;
  [SerializeField] float yRange = 8f;
  [SerializeField] float positionPitchFactor = -2f;
  [SerializeField] float positionYawFactor = -2f;

  [SerializeField] float controlPitchFactor = -10f;
  [SerializeField] float controlRollFactor = -20f;

  float yThrow;
  float xThrow;
  void Update()
   {
      ProcessTranslation();
      ProcessRotation();
   }

  private void ProcessRotation()
  {
    float pitchDueToPosition =  transform.localPosition.y * positionPitchFactor;
    float pitchDueToControlFlow = yThrow * controlPitchFactor;

    float pitch = pitchDueToPosition + pitchDueToControlFlow;
    float yaw = transform.localPosition.x * positionYawFactor;;
    float roll = xThrow * controlRollFactor;

    transform.localRotation = Quaternion.Euler(pitch,yaw,roll);
  }

  private void ProcessTranslation()
    {
      xThrow = Input.GetAxis("Horizontal");
      yThrow = Input.GetAxis("Vertical");

      float xOffset = xThrow * Time.deltaTime * offsetSpeed;
      float rawXPos = transform.localPosition.x + xOffset;

      float yOffset = yThrow * Time.deltaTime * offsetSpeed;
      float rawYPos = transform.localPosition.y + yOffset;

      float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
      float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

      transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }
}

Hi ByJingo,

First of all, check if the Timeline animation accidentally animates the Player Ship that you also move via code. The spaceship must not be animated.

Secondly, comment out the pitch/yaw/roll lines. The ship is supposed to move without the rotation. Does the ship still automatically rotate to a weird angle?

If not, add the pitch, test your game, add the yaw, test your game, and so on. Test your game step by step until the problem occurs. Once you found the culprit, test a value closer to 0. Maybe -20f is too much.

Thanks for the suggestions, unfortunately it gets weirder:

I’ve set the animation to mute and set the variables to 0 like this:

    float pitch = 0; // pitchDueToPosition + pitchDueToControlFlow;
    float yaw = 0; // transform.localPosition.x * positionYawFactor;
    float roll = 0; // = xThrow * controlRollFactor;

and it still jumps to that strange 90 degree angle (more if, say “20” is hardcoded into

I think I’ve narrowed it down to pitch being the problem, as setting roll and yaw to about 20 work as expected (slight tilts).

If I hard code pitch to -45 to corrects it but I don’t understand why it’s just on the x axis and what is forcing the angle

There could be an issue with the conversion. Unity internally works with Quaternions, we use Euler angles. Unity converts the values back and forth. The result could be, for example, 90, then -270. Both represent the same angle. And then you end up getting unexpected results. That’s an unsolveable mathematical problem.

And another problem could be the infamous gimbal lock, which you mentioned in your initial post and which is the reason why I asked you to comment the pitch/yaw/roll lines out instead of setting the values to 0. By commenting them out you could test each axis separately. If the issue does not occur if you rotate only one axis, it was not caused by the gimbal lock.

Privacy & Terms