Rotations messing up?

I have this scrip attached to camera and when the camera goes below 60 X rotation the y and z change to 180 and changes to some random angle which i didn’t set why is this happening?

void Update () {
        float cameraRotation = Input.GetAxis ("Mouse Y");
        transform.Rotate(-cameraRotation * rotationSpeed, 0, 0);
        if(transform.localRotation.eulerAngles.x > 60) transform.Rotate (60,0,0);
    }

Rotations are stored as quaternions, and there’s more than one valid way of converting quaternions to euler angles (e.g., 0, 0, 0 is the same as 180, 180, 180). The rotations aren’t messing up, they’re correct. Reading only one axis of euler angles is not advisable, because out of context with the other two it’s not very meaningful.

–Eric

So what can i do to fix this then?

I’m not really sure what you’re trying to do.

–Eric

Store your rotation angle separately and explicitly set it.

Up at the top of your class with the other variables:

protected float cameraRotation = 0.0f; // assuming you want to start at 0

Then your Update:

void Update()
{
     cameraRotation += Input.GetAxis("Mouse Y") * rotationSpeed;
     
     if(cameraRotation > 60)
          cameraRotation = 60;

     transform.localRotation.eulerAngles = new Vector3(cameraRotation, transform.localRotation.eulerAngles.y, transform.localRotation.eulerAngles.z);
}

I’m getting this error: error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localRotation’. Consider storing the value in a temporary variable
I’m not that good with rotations so i don’t know how to handle this.

What i’m trying to do is get the camera to stop rotating down if it goes too far to prevent the player camera from doing a full 360 rotation.

?

You can’t change it directly, you have to save it to another variable, change it there, then assign it back again.

Quaternion tempRotation = transform.localRotation;
tempRotation.eulerAngles = new Vector3(cameraRotation, transform.localRotation.eulerAngles.y, transform.localRotation.eulerAngles.z);
transform.localRotation = tempRotation;

or something like that.

Thanks this works now.