Rotation Problem

Hi, I have a problem with rotation or most Specifically Euler Angels, I want to put an if statement that says if the X rotation is smaller than 90 then you can execute the if statement so I wrote

if (transform.localEulerAngels.x <90)


// orders


but when the game object Euler angel on the X-axis is after 90 degree the X value Starts decreasing and the if statement orders continue and the script fails, anybody has a good idea on how to do it, and thanks.

[This is my script]

using UnityEngine;

public class Air_Plane_Script : MonoBehaviour {

public float RollSpeed;
public float PitchSpeed;
public float PlaneSpeed;
public float Gravity;
public float DesiredRotationX;

private float RollInput;
private float PitchInput;
public float YawSpeed;
private bool Change = true;

void Update()
void ApplyPlaneControls()
    transform.position += transform.forward * PlaneSpeed*Time.deltaTime;

    RollInput = Input.GetAxisRaw("Horizontal")*RollSpeed*Time.deltaTime;
    PitchInput = Input.GetAxisRaw("Vertical")*PitchSpeed*Time.deltaTime;

void ApplyPlanePhysics()
    PlaneSpeed -= transform.forward.y * Gravity * Time.deltaTime;
    if (PlaneSpeed < 10 && // Here is where i want to put the if statement that i asked the    question for)
        Change = false;
        DesiredRotationX += 50f * Time.deltaTime;
        transform.localEulerAngles = new Vector3(DesiredRotationX, transform.localEulerAngles.y,transform.localEulerAngles.z);
        Change = true;

    if (PlaneSpeed < 1)
        PlaneSpeed = 1;


The problem with rotations is that are not straightforward in the 3d plane. There can be more than one euler angles representation to notate the same rotation in 3D. Thats why unity uses Quaternions to represent the angles.

What i guess is happening to you is the following: After you manually change your euler angles in transform.localEulerAngles = new Vector3(DesiredRotationX, transform.localEulerAngles.y,transform.localEulerAngles.z);, unity recalculate them in terms of quaternions and in some cases (that are not arbitrary) your angles get flipped in some way that leads you to the same rotation but with a different eulerAngles.x.

In the next iterarion, when you ask for the rotation in the x axis you are not guaranteed to get the same x value that u have set before.

How to solve the problem. (Note that this will work only for 1 axis rotation. I encourage you to use Quaternions instead of euler angles for 3d rotations. )
Lets say u know your default euler angles for Y and Z are: Y = 0, Z = 0. You will notice after changing the X value that at some point the Y and Z value will become different, lets say Y=-90, z=90, your X axis will become lets say 180 - desiredX at which point the new Y and Z values will stay and the X value will be allways (to continue with the example) 180 - desiredX

If your problem present the behaviour that I have explained the solution is straightforward:

  1. before your current IF statement, initialize a bool lets call it standardYZ if the Y and Z axis are the standard

  2. Modify the if statment adding this in the right side of the && operator: ((standard && x > minXForRotation) || (!standard && 180 - x < minXForRotation)) Take into acount that the not standard clausule may represent the change that unity is making to your x axis. Now you know if you must rotate

  3. Change the update of angles from transform.localEulerAngles = new Vector3(DesiredRotationX, transform.localEulerAngles.y,transform.localEulerAngles.z); to transform.localEulerAngles = new Vector3(DesiredRotationX, standardY, standardZ);

Here’s a simple demonstration to make sure that you are rotating the object on the right axis

And, here’s a very good way to check for X rotation is more than 90d

if (transform.rotation.eularAngles.x > Quaternion.Eular(90f, 0f, 0f).x)
	// do something

Hope this helps.