transform.eulerAngles issue.

Hello. I am making a flappy bird clone. I have a problem with rotating the bird object to 45 degrees.
The code works fine and it’s rotating the bird up to 45 degrees as long as the transform.eulerAngles.x is positive.
As soon as it goes to negative value it stops working.
Can anyone explain why?
Do conditional statements work differently for negative rotation values?

This is the code :

public class ByrdController : MonoBehaviour
{
    public int score = 0;
    private Rigidbody rb;
    [SerializeField] float jumpForce = 5.0f;
   
    private void Awake()
    {
        //just to place the bird straight forward
        transform.eulerAngles = new Vector3(0f, 90f, 0f);
    }

    void Start()
    {
        //used for jump
        rb = GetComponent<Rigidbody>();      
    }
  
    void Update()
    {
        if (transform.eulerAngles.x <= 45f)
        {
            //it rotates it downwards as the bird falls up to 45 degrees
            transform.Rotate(1f, 0f, 0f);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Jump();
        }
    }

    public void Jump()
    {
        //makes the bird jump
        rb.velocity = Vector3.up * jumpForce;

        //points bird upwards as it jumps
        //if i set x value to positive value it works fine
        //if i set it negative it stops rotating and stays in the set value (-45f in this case)
        transform.eulerAngles = new Vector3(-45f, 90f, 0f);
    }

}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

The problem you report is likely from gimbal lock.

Notes on clamping camera rotation and NOT using .eulerAngles because of gimbal lock:

How to instantly see gimbal lock for yourself:

1 Like

Thanks for a quick reply. I will try fixing it tomorrow using your method and bypassing eulerAngles.
Will post results. Night.

1 Like

Couldn’t sleep and i had to fix it. It was very easy thanks to your help Kurt-Dekker.

public class ByrdController : MonoBehaviour
{
    public int score = 0;
    private Rigidbody rb;
    [SerializeField] private float jumpForce = 5.0f;
    private float xValue;

    void Start()
    {
        xValue = 0f;
        rb = GetComponent<Rigidbody>();
        transform.rotation = Quaternion.Euler(xValue, 90f, 0f);
    }
  
    void Update()
    {
        transform.rotation = Quaternion.Euler(xValue, 90f, 0f);

        if(xValue <= 44)
        {
            xValue++;
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Jump();
        }
    }

    public void Jump()
    {
        rb.velocity = Vector3.up * jumpForce;
        xValue = -45f;
    }

}
1 Like

Amen Goto… amen. Been there, even had the lights OUT before, and got up and fixed what needed fixing.