Making Godot Third Person Camera Controller

In plenty of my games, I have to make a third person camera, especially for TPS games (third person shooters) or for testing my game quickly. I am a fan of GTA San like camera controller. In gta sa, it follows player, and if we move mouse, it orbits around player, in both horizontal & vertical axes.

This Godot recipe assumes that you are familiar with the Godot engine.

TPS Camera

In third person camera, we have a camera following the player, from a distance (radius). In annoying games, it is fixed so we can only view from behind, but in some games, it is allowed to move the mouse to view from any angle. In Godot, TPS camera is easy to make.

Make a Camera Scene

We start by making a scene for the camera. The root node should be Node3D (basic spatial node) & name it CameraSystem. Add another Node3D as its child & name it Arm. Move the Arm away from origin, depending on how far you want your camera to be.

After that, add Camera3D as a child of Arm. Now attach a script to the root Node3D (aka Camera System). In the script, we will capture mouse motion & rotate the Arm only, the camera will automatically follow its parent node as it is attached to it.

extends Node3D

# Capture/un-capture the cursor (show or hide cursor)
func _physics_process(delta):
	if Input.is_action_just_pressed("ui_cancel"):
		if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		else:
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

# If cursor is captured (hidden), rotate the root node along y-axis
# and the arm around x-axis
func _input(event):
	if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
		var mouse_delta = event.relative
		rotate_y(deg_to_rad(-mouse_delta.x * 0.1))
		$Arm.rotate_x(deg_to_rad(mouse_delta.y * 0.1))

In this example, pressing ESC key (ui_cancel) will cause cursor to appear to disappear. The TPS camera will be active only when the cursor is captured. In your game, you will likely keep the camera captured until the user goes to the main menu. So you will likely open the main-menu if user presses ESC key & the main menu scene itself will cause the cursor to be un-captured.

Other Camera Types

  1. You might want FPS camera (first person camera controller).
  2. You might want top-down RTS camera as in Stronghold, Age of Empires and similar games.

Thank you for reading <3

Leave a Reply

Your email address will not be published. Required fields are marked *