transform.localRotation resets after rotation

Hello! I’ve been trying to rotate a game object on its local x-axis for a while now, and have no idea what is wrong with my code. Whenever I look up or down, the objects rotates where its supposed to be, but seems to go back to its original (0,0,0) position during the next couple of frames. I used Debug.Log to check it’s values, and it seems like even though in the editor the values change during rotation, when the Update function is executed, the rotation x-axis rotation of the object is always extremely close to 0.

When i replace “transform.localRotation.x” in the first line of the Update function with “transform.localEulerAngles,” rotation works as desired, but up to 0 degrees. (Known issue with Eulers)

Could somebody please explain why “transform.localRotation.x” always returns “0”?

public class PlayerRotator : MonoBehaviour {
		float flRotationSpeed = 150;
		float _xRotation;


	void Update () {
		_xRotation = Mathf.Clamp(transform.localRotation.x + (-Input.GetAxisRaw("Mouse Y") * flRotationSpeed * Time.deltaTime), -85, 85);

		Quaternion rotation = Quaternion.Euler(_xRotation,0,0);


		transform.localRotation = rotation;
	}
}

Euler angles do not except numbers greater than 360 or less than zero. it would be easier in this case to simply use transform.rotate but if you wanted to directly change the angle in eulers you would do it like this with a correction:

float rot;
	float speed;
	void Start(){
		speed = 10;
		rot = transform.localEulerAngles.x;
	}
	
	void Update () {
		rot = rot -Input.GetAxisRaw ("Mouse Y") * speed * Time.deltaTime;
		if(rot>360f){rot-=360f;}
		if(rot<0f){rot+=360f;}
        
		transform.localEulerAngles = new Vector3(rot,transform.localEulerAngles.y,transform.localEulerAngles.z);
	}

in other words, your script is trying to push a rotation below zero. the PC still thinks its zero. a rotation 5 degrees below 0 would actually be 355. just like the hands of a clock passing 12… there is no 13 O’clock. rotation numbers are represented 0 to 360. anyways, Quaterntons will regester numbers outside of that but eulers will not. but eulars are much easier to work with cause quaterntons are much more complicated. if you use transform.rotate, the friendly developers at unity consider all this for you in their function and you don’t need to worry about all this sillyness when rotating an object. but its still good for you to know