Making a camera that follows the player
At the moment, our character can run around, but at some point, it will run off the screen and get lost forever. Our in-game camera should follow them around so the player knows where they are.
Luckily, the Godot Engine has a pretty nice camera system that we can use. It can be a bit basic, but it is all we need, and with some extra nodes, we’ll be able to achieve a very smooth-moving camera.
Setting up a basic camera
For 2D games, Godot provides the Camera2D node.
Open up the player.tscn
scene and add a Camera2D node to the Player
node. This is all we need to make a basic camera that follows the player:
Figure 9.1 – The player scene with an added Camera2D node
But this basic camera feels a bit stiff; it starts and stops moving at exactly the moment the character does. This doesn’t feel very natural. Let’s see how to solve this.
Adding drag margins
To make the camera...