Do not rotate if x axis if target rotation x axis is over 50

I’m simply trying to prevent the object from rotating past a certain limit but still maintain rotation update. However unity is not allowing me to use the code inside the if statement because it isn’t a variable. How would I be able to make sure that the rotation doesn’t exceed it’s treshold?

        targetRotation = Quaternion.LookRotation(enemyTarget.transform.position - transform.position);
            if(targetRotation.eulerAngles.x > 50)
            {
                targetRotation.eulerAngles.x = 49;
            }
        rb.MoveRotation(targetRotation);

Don’t read / write from .eulerAngles. Due to gimbal lock they are not reliable.

Instead:

  • keep your own float angle variable.

  • adjust that variable up/down

  • clip that variable to a range

  • set the rotation with Quaternion.Euler()

Here’s an example of using floats to drive rotations:

Turret aiming/rotating:

1 Like