FPS Camera tilt causing mouse look to go past Clamp angle

Godot Version

Godot4 Git Master Branch

Question

I added camera tiliting when strafing in my fps game however,

Everything works perfectly until i added the following for camera tilt when strafing:

    if input_dir.x < 0:
        camera.rotation.z = lerp_angle(camera.rotation.z, deg_to_rad(5), 10 * delta)
    elif input_dir.x > 0:
        camera.rotation.z = lerp_angle(camera.rotation.z, deg_to_rad(-5), 10 * delta)
    else:
        camera.rotation.z = lerp_angle(camera.rotation.z, deg_to_rad(0), 10 * delta)
func update_camera():
    rotate_y(deg_to_rad(rotation_input))
    camera.rotate_x(deg_to_rad(tilt_input))
    camera.rotation.x = clamp(camera.rotation.x, -PI/2, PI/2)

Before i added the strafing tilt, the camera would lock at 90 degrees up or down and everything was fine. But as soon as you strafe now you can look down or up past 90 degrees and the camera flips directions on the Z axis ignoring the clamp. (facing backwards, forward is now backwards, down is up etc..)

i have a feeling its something to do with transforms and i have tried to orthonormalize every frame with no luck. Even setting the clamp value to deg_to_rad(85) doesnt work.

Any ideas?

Thanks in advanced!

Just found that setting the camera rotation order to XYZ instead of default YXZ seems to solve the problem.

I dont think this is a proper fix though as i must be doing something out of order or incorrectly as the default rotation order should work i would think