A Camera That Follows
Part of the Game Feel section of Coddy's Godot journey. Lesson 8 of 23.
This level is 1600 pixels wide, but the window shows only 640. When the robot walks right, it leaves the screen. A Camera2D node decides which part of the level is on screen.
The view is centered on the camera. Make the camera a child of the player, and it moves with the player. The player then stays in the middle of the screen.
var camera = Camera2D.new()
$Player.add_child(camera)Camera2D.new() makes a camera node, and add_child puts it under the player. The first camera added to the game becomes the one the screen uses.
Challenge
EasyMake the camera follow the robot.
- In
level.gd, make aCamera2DwithCamera2D.new(). - Add it as a child of
$Player.
Press Play and walk right with the arrows or D: the robot stays in the middle. Then Check.
Try it yourself
extends Node2D
func _ready():
pass
All lessons in Game Feel
Practice on your own: Godot playground