First-Person Player in Godot 4
A first-person controller is one of those systems that looks complicated at first, but the core idea is surprisingly simple.
In Godot, our player can inherit from CharacterBody3D:
class_name Player
extends CharacterBody3D
CharacterBody3D is designed for characters that need to move through a 3D environment while interacting with the physics system.
By giving the class a name with class_name Player, we can also refer to this script as a Player type elsewhere in our project.
The player itself is responsible for two different kinds of movement:
- Body movement: walking forward, backward, and sideways.
- Looking around: rotating the player horizontally and the camera vertically.
Keeping these two systems separate makes the controller easier to understand and extend.
The player's camera is stored with an @onready variable:
@onready var camera: Camera3D = %Camera3D
The %Camera3D syntax refers to a node marked as Unique Name in Owner in the scene.
This means we don't have to search for the camera every time we need it. Once the scene is ready, the camera variable points directly to our Camera3D.
The result is a clean relationship:
Player
└── Camera3D
The player controls the body's horizontal rotation, while the camera handles looking up and down.
This distinction becomes particularly important when building a first-person controller.