The reason the clamping is not working is because the mouse is moving fast enough that when the mouse motion is converted into a high enough value that when it is applied to the rotation of the camera it bumps the value past the range in the clamp
function.
Also, if I recall correctly, Godot reset the rotation on the X axis once it gets past a certain point, which makes figuring out whether the camera has rotated past the clamping point harder to calculate.
However, since when the tutorial was written I have learned a few things about clamping and I think I know how to fix the problem. Ideally you want to check for clamping before you apply the rotation to the camera (or in the case of the FPS tutorial, rotation_helper
).
I think the following code should fix the problem:
func _input(event):
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
var camera_rot = rotation_helper.rotation_degrees
var rotation_to_apply_on_x_axis = (event.relative.y * MOUSE_SENSITIVITY);
if (camera_rot.x + rotation_to_apply_on_x_axis < -70):
camera_rot.x = -70
elif (camera_rot.x + rotation_to_apply_on_x_axis > 70):
camera_rot.x = 70
else:
camera_rot.x += rotation_to_apply_on_x_axis;
rotation_helper.rotation_degrees = camera_rot
The big difference here over the code in the FPS tutorial is that we are checking and clamping the value before we apply it to rotation_helper
.
This means that we will not have to worry about Godot reset the rotation, which is one of the biggest headaches using the method in the FPS tutorial.
I should add, that I cannot reproduce the issue in the FPS tutorial, even with moving my mouse very fast at a high sensitivity, but the code I posted above seems to work the same as the code in the FPS tutorial, but unlike the code in the FPS tutorial it should be able to handle the really fast movements that I think were causing the clamp
function to not work.
Hopefully this helps :smile: