Issue where camera is flipping even when Clamped

Godot Version

4.5.1

Question

Hello, I have been trying to create a first person 3D game, however I have found a issue in my code for the camera. If I move the camera fast enough either up or down the camera turns to the oposite side, however without turning anything else.

For example, if I am looking somewhere where the head rotation is 45 degrees, if I perform this bug on the opposite side it will still be 45 degrees, meaning the controls will be flipped and everything else will still think I am turned to the other side, even though the camera rotation is clamped

I have tried decreasing the maximum you can go up or down, going as low as to clamping from -5 to 5 degrees, however I could still perform this bug, and am out of ideas for fixing it. How could I resolve this?

if event is InputEventMouseMotion:
		head.rotate_y(-event.relative.x * SENSITIVITY)
		camera.rotate_x(-event.relative.y * SENSITIVITY)
		camera.rotation.x =clamp(camera.rotation.x,deg_to_rad(-80),deg_to_rad(80))

Assuming that your rig is such that camera only rotates around x axis, don’t use rotate_x(), instead get the current rotation.x, add delta rotation to it, clamp the result and then assign it back to rotation.x