How do I have a 3D camera smoothly follow an object

Godot Version

4.2.2

Question

Hi, I’m new to Godot in 3D and I’m making a racing game. I want a camera to smoothly follow the car instead of being attached to it and following all of its movements. How can I do this?

Two approaches:

  1. This is a common problem that other people have solved before you - there are addons for this. I recommend the addon PhantomCamera, but there are probably others as well.
  2. If you’d rather learn how to code this kind of thing yourself (which is a good exercise), then you’ll want to not make the camera a child of the car (or just tick the little “top level” checkbox on the camera to make it behave as if it isn’t). Then give the camera a script with some variables to keep track of where it wants to be, and where it wants to look:
extends Camera3D

@export var follow_target: Node3D
@export var look_target: Node3D

Those two nodes can be children of the car object - these are the ones we’ll try to smoothly follow.

Then, you can start with a script that just keeps the camera right on the follow target - this is not the end result you want, just a starting point:

func _process(delta):
  global_position = follow_target.global_position
  look_at(look_target)

From there, you can use functions like lerp, move_toward, and similar to smooth out the movement, rather than just setting the camera’s position to the follow target’s position.

2 Likes