karvacode

Implementing Mouse Look in Godot 4

Once we have mouse movement, we can turn it into camera rotation.

Our controller uses two separate rotations:

rotate_y(-event.relative.x * mouse_sensitivity)
camera.rotate_x(-event.relative.y * mouse_sensitivity)

The first line rotates the player around the Y axis.

rotate_y(...)

This controls looking left and right.

The second line rotates the camera around its X axis:

camera.rotate_x(...)

This controls looking up and down.

Why use two different objects?

Because we don't want the entire player body to tilt when looking up or down.

Imagine the player looking at the ceiling. Their body should still be standing upright:

        Camera
           ↑
          / 
         /
      Player
        │
        │
        │

The player rotates horizontally, while the camera rotates vertically.

This gives us the familiar first-person behavior:

Player Y rotation
      ↕
 left ↔ right
      ↕

Camera X rotation
     up
     ↕
     ↔
     ↕
    down

The mouse_sensitivity variable controls how much rotation we get from each pixel of mouse movement:

@export var mouse_sensitivity: float = 0.002

Because it is exported, we can adjust the value directly in the Godot Inspector without modifying the script.

A smaller value makes the camera feel slower and more precise.

A larger value makes the camera turn faster.

This separation between horizontal player rotation and vertical camera rotation is one of the fundamental patterns behind a traditional FPS controller.