TExcelTAD
Home › Godot 4 with GDScript › Camera2D

Camera2D

Without a camera, a 2D Godot scene shows the area from (0, 0) to the window size, and anything further right is off screen. A Camera2D moves the view: whichever camera is current decides what the viewport shows. Make it a child of the player and the view follows the player; set limits and it stops at the level edges; add smoothing and it glides instead of snapping.

Setting one up

  1. Open your player scene and add a Camera2D as a child of the root CharacterBody2D.
  2. In the Inspector, make sure Enabled is on. The first enabled camera that enters the tree becomes the current one; call make_current() to switch cameras from code.
  3. Under Limit, set Left/Top/Right/Bottom to your level's edges in pixels.
  4. Under Position Smoothing, tick Enabled and set Speed (5 is a gentle follow).
  5. Turn on Drag > Horizontal/Vertical Enabled for a dead zone: the player can move inside the middle of the screen before the camera follows.
  6. To see the limits and the dead zone in the 2D viewport, tick Editor > Draw Limits and Draw Drag Margin in the camera's Inspector. Then run the game.

In a .tscn file the camera is just a node with properties:

ini
[node name="Camera2D" type="Camera2D" parent="."]
limit_left = 0
limit_top = 0
limit_right = 3200
limit_bottom = 720
position_smoothing_enabled = true
position_smoothing_speed = 5.0
drag_horizontal_enabled = true

Migrating from Godot 3: current = true became enabled plus make_current(), smoothing_enabled became position_smoothing_enabled, and zoom is inverted: in Godot 4, zoom = Vector2(2, 2) makes everything look twice as big (zoomed in).

Following with smoothing and limits

The headless test runner's viewport is only 64×64 pixels, so the view centre can't go closer than 32 px to a limit. A player runs right at 300 px/s; the camera is its child, with smoothing and a level that is 200 px wide:

gdscript · try it yourself
extends Node2D

# frames: 61
var player: Node2D
var cam: Camera2D

func _ready():
	print("viewport size: ", get_viewport_rect().size)
	player = Node2D.new()
	add_child(player)
	cam = Camera2D.new()
	cam.limit_left = 0
	cam.limit_top = 0
	cam.limit_right = 200
	cam.limit_bottom = 64
	player.add_child(cam)
	cam.make_current()
	cam.position_smoothing_enabled = true
	cam.position_smoothing_speed = 5.0
	print("is current: ", cam.is_current())

func _process(delta):
	player.position.x += 300 * delta
	var f := Engine.get_process_frames()
	if f % 10 == 0:
		print("frame ", f, ": player x ", snappedf(player.position.x, 0.1), ", view centre x ", snappedf(cam.get_screen_center_position().x, 0.1), ", target x ", clampf(player.position.x, 32, 168))
viewport size: (64.0, 64.0)
is current: true
frame 0: player x 5.0, view centre x 32.0, target x 32.0
frame 10: player x 55.0, view centre x 32.0, target x 55.0
frame 20: player x 105.0, view centre x 54.7, target x 105.0
frame 30: player x 155.0, view centre x 99.0, target x 155.0
frame 40: player x 205.0, view centre x 146.7, target x 168.0
frame 50: player x 255.0, view centre x 168.0, target x 168.0
frame 60: player x 305.0, view centre x 168.0, target x 168.0

Read it in two steps:

  1. Smoothing moves the camera's internal position a fraction of the way towards its target every frame (exponential smoothing), so it lags behind a moving player and catches up when the player stops.
  2. Limits then clamp the visible rectangle. The view centre stays at 32 until the smoothed position passes 32, and stops at 168 (200 − 32) however far the player runs.

get_screen_center_position() is where the camera really looks after smoothing, limits and offset. global_position is only where the camera node is.

Setting limits from the level instead of typing numbers keeps them correct when you edit the map:

gdscript
func fit_camera_to(map: TileMapLayer, cam: Camera2D) -> void:
	var used := map.get_used_rect()                 # in cells
	var cell := map.tile_set.tile_size
	cam.limit_left = used.position.x * cell.x
	cam.limit_top = used.position.y * cell.y
	cam.limit_right = used.end.x * cell.x
	cam.limit_bottom = used.end.y * cell.y

Screen shake and teleports

offset moves the view without moving the camera node, which makes it the place for screen shake. A good shake uses a decaying "trauma" value and squares it, so small hits barely shake and big hits shake a lot. Seed the random numbers (here the global generator with seed(); a game would give the shake its own RandomNumberGenerator, or sample a FastNoiseLite for smoother motion) so a replay shakes the same way:

gdscript · try it yourself
extends Node2D

# frames: 12
var cam: Camera2D
var trauma := 0.0            # 0..1, decays over time
const MAX_OFFSET := 8.0      # pixels
const DECAY := 2.0           # trauma lost per second

func _ready():
	seed(42)                 # fixed seed: the same shake on every run
	cam = Camera2D.new()
	cam.position_smoothing_enabled = true
	add_child(cam)
	cam.make_current()
	add_trauma(0.6)          # e.g. the player was hit

func add_trauma(amount: float):
	trauma = minf(trauma + amount, 1.0)

func _process(delta):
	trauma = maxf(trauma - DECAY * delta, 0.0)
	var shake := trauma * trauma            # squared: strong hits shake much more than light ones
	cam.offset = Vector2(randf_range(-1, 1), randf_range(-1, 1)) * MAX_OFFSET * shake
	var f := Engine.get_process_frames()
	if f % 2 == 0:
		print("frame ", f, ": trauma ", snappedf(trauma, 0.01), ", offset ", cam.offset.snappedf(0.01))
	if f == 10:
		cam.position = Vector2(500, 0)      # teleport, e.g. respawn at a checkpoint
		cam.reset_smoothing()               # jump there instead of gliding across the level
		cam.force_update_scroll()
		print("after teleport, view centre x = ", snappedf(cam.get_screen_center_position().x, 0.1))
frame 0: trauma 0.57, offset (-1.96, -1.57)
frame 2: trauma 0.5, offset (1.75, -0.72)
frame 4: trauma 0.43, offset (-1.46, -0.9)
frame 6: trauma 0.37, offset (0.57, -0.23)
frame 8: trauma 0.3, offset (0.04, 0.13)
frame 10: trauma 0.23, offset (-0.25, 0.31)
after teleport, view centre x = 499.8

With smoothing on, moving the player to a checkpoint makes the camera glide across the whole level. reset_smoothing() snaps it to the new position; force_update_scroll() applies the change immediately instead of at the end of the frame. The small difference from 500 is the shake offset (-0.25).

Always give players a "reduce screen shake" option (multiply MAX_OFFSET by a setting). Shake makes some people feel sick.

More camera features

Property / method Use
zoom Vector2(2, 2) = 2× magnification. Change it with a tween for smooth zooms
drag_left_margin ... size of the dead zone, as a fraction of half the screen
anchor_mode ANCHOR_MODE_DRAG_CENTER (default, position is the centre) or ANCHOR_MODE_FIXED_TOP_LEFT
ignore_rotation on by default: the view doesn't rotate with a rotating parent
process_callback CAMERA2D_PROCESS_IDLE (default) or CAMERA2D_PROCESS_PHYSICS; use physics when following a physics body whose movement looks jittery
get_screen_center_position() the real centre of the view this frame

For multiple targets, priorities, cinematic transitions and more follow modes, the Phantom Camera addon builds on Camera2D/Camera3D (see AssetLib & Addons). Split screen uses one SubViewport plus its own camera per player.

Test yourself

In Godot 4, what does camera.zoom = Vector2(2, 2) do?

  1. Shows twice as much of the world (zoom out)
  2. Makes everything look twice as big (zoom in)
  3. Doubles the camera speed
  4. Nothing unless smoothing is on

The player respawns at a checkpoint far away and the camera slowly flies across the level. What fixes it?

  1. Disable limits
  2. Call reset_smoothing() on the camera after moving the player
  3. Set zoom to 0
  4. Use _physics_process

Where should screen shake be applied?

  1. To the player's position
  2. To the camera's offset, which moves the view without moving the camera node
  3. To the TileMapLayer
  4. To limit_left