Camera stops rotating when mouse cursor reaches edge of screen

Godot Version 4.3

Question

Hello,

I’m making a 3D game in first person perspective.

The movement of the mouse is supposed to control the rotation of the camera. I’m using the event.relative of the InputEventMouseMotion for that. It works, the movements of the mouse now correctly control the yaw and pitch rotation of the camera. But there is a problem. The camera stops rotating when the mouse cursor reaches the edge of the screen. Even when I keep moving my mouse event.relative stays zero at the edge of the screen.

I made the mouse pointer invisible with Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN), but that does not solve the problem.

What is the proper way of solving this?

Try this instead:

Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
2 Likes

Thank you very much. That worked like a charm. And only one line of code!

1 Like

Yea the HIDDEN property doesn’t change its capture-ability:

MouseMode MOUSE_MODE_CAPTURED = 2
Captures the mouse. The mouse will be hidden and its position locked at the center of the window manager’s window.
Note: If you want to process the mouse’s movement in this mode, you need to use InputEventMouseMotion.relative.

MouseMode MOUSE_MODE_CONFINED_HIDDEN = 4
Confines the mouse cursor to the game window, and make it hidden.

You’re on the right track, but the behavior you’re experiencing is because MOUSE_MODE_HIDDEN only hides the mouse cursor—it doesn’t stop it from being confined by the edges of the screen.

What you need is capturing the mouse, so the cursor is locked to the center of the screen and effectively becomes “infinite” for camera rotation. This is a common setup for first-person camera controls in 3D games.

Use Input.MOUSE_MODE_CAPTURED instead of Input.MOUSE_MODE_HIDDEN.

Here’s how to do it:

gdscript
Copy
Edit
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

Best Regards,
Darkforce21