How to Track Players with Godot’s Camera

You can access the full course here: Godot Game Development for Beginners

In this lesson, we’re going to be creating a camera in Godot and having it track our player horizontally.

Create a new node of type Camera2D in the main scene.

Godot menu to Add Child Node to MainScene Node

Godot Create New Node window with Camera2D selected

 Select the Camera2D and enable the ‘Current‘ property in the inspector.

Godot Inspector with Current ticked to On for Camera2D

You will see that the blue outline appears in the viewport, showing the range of the current camera.

Godot Viewport with blue outline for camera

Now, let’s make our camera to track our player. Click and drag the camera to where we want it to be initially.

Godot Camera2D with initial position

Create a new script inside Camera2D and name it as CameraController.

Script selected for Camera2D in Godot

Attach Node Script window in Godot

 What we have to do inside the script is simply find our player by using get_node(), and then set the camera’s x position to be the same as the player’s x position.

extends Camera2D

onready var player = get_node("/root/MainScene/Player")

#This function gets called every frame
func _process (delta):
	
	position.x = player.position.x

Save the script and press the Play button.

Godot platformer with 2D camera tracking character

The camera now tracks us along the x position.

 

Transcript

Welcome back, everyone. In this lesson, we’re gonna be creating a camera in Godot and having it track our player horizontally.

So in our main scene here, I’m gonna create a new node, and this is gonna be of type Camera2D. Now, this Camera2D, as you can see, it appears in our main scene, and it has that blue rectangle similar to the one that already exists. So what we can do is, we can actually have this camera override the existing preset window, which is what we see into the world, by going over to the Inspector with the Camera2D selected and click on Current. Make sure Current is enabled, and you’ll see that the blue outline suddenly glows.

Now if we press play, you’ll see that the camera has changed, but right now, it is not moving to track our player. So in order to do that, let’s first of all move our camera over to where we want it to be initially. So we’ll drag it–we’ll select the camera here, and we’ll drag it down to here, where it overlays the existing camera. Might need to move it up just a bit. There we go.

And now what we can do is, if we go to the Inspector, we can create a new script for this camera, so we’ll go new script here, and I’m gonna rename this to Camera Controller. We’ll create that, and here we go.

So all we’re gonna do in this script right here is two things. We’re gonna create a variable. So we’ll go onready var player, and this is going to be equal to get underscore node, and we wanna find the player node. Now, to do that, we need to go to the root of our scene, so we’ll go slash root slash main scene slash player, and this is gonna find our player node. Then if we go func underscore process delta. Now, this gets called every frame. We just wanna go position dot X equals player dot position dot X, and this will track us along our X position.

So if you press play now, you’ll see that the camera now tracks us along the X position but the Y stays the exact same like so.

Interested in continuing? Check out the full Godot Game Development for Beginners course, which is part of our Godot Game Development Mini-Degree.