I am using code from the Godot Docs' FPS Tutorial to create a simple first person controller. It's all working quite well, except that the mouse movement clamp that is supposed to stop the player form being able to turn their head upside-down is not working. There is some resistance but if you look up or down quickly and/or repeatedly, it stops working almost immediately.

Below I will include a link to the tutorial I mentioned, the player code I am using, a short video of the issue and finally my Godot project files. I have temporarily disabled looking left and right, in order to highlight the issue. If someone could take a look, I'd be very grateful!

Tutorial: LINK

Project code: LINK

Video of issue: LINK

Project files: LINK

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:

That's working great! What you said makes sense, as well. I'm not a very experienced programmer, unfortunately. I get the concepts and I can just about understand what is going on in this code but I struggle to come up with my own solutions to problems because I just don't have a firm enough understanding of how the language works. I guess it all comes down to education and practice. Thank you so much for your quick (and very useful) response! :)

@skyoung said: That's working great! What you said makes sense, as well. I'm not a very experienced programmer, unfortunately. I get the concepts and I can just about understand what is going on in this code but I struggle to come up with my own solutions to problems because I just don't have a firm enough understanding of how the language works. I guess it all comes down to education and practice.

Don’t worry, I was in exactly the same position as you a few years ago. I knew how many of the concepts, almost all of the basics of programming, but putting all of that knowledge together so I could solve problems proved difficult. Eventually it all sort of just clicked, I’m not really sure what happened.

Just keep practicing and don’t give up! :smile:

Thank you so much for your quick (and very useful) response! :)

No problem, happy to help!

Just keep practicing and don’t give up! :smile:

Thank you for the encouragement! _

4 years later