Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 Β· 2D Games

Camera and Parallax in 2D

Follow the player with Camera2D in Godot 4: smoothing, limits, zoom, screen shake, resolution scaling and Parallax2D layers, with Python stand-ins for lerp, shake and parallax maths.

Without a camera, Godot draws the world from its origin. A Camera2D makes the view follow a target, zoom in, stay inside the level and shake when something explodes. Combined with parallax backgrounds it gives a flat 2D scene a sense of depth.

Godot code is shown read-only (the engine cannot run on this site). The Python blocks are stand-ins that reproduce the maths so you can see what the engine does with your settings. Before this lesson, read player movement.

Adding a Camera2D

The simplest setup is to make Camera2D a child of the player. It inherits the player's position, so it follows automatically. Make sure Enabled is on (it becomes the current camera of its viewport). Useful properties:

  • zoom: Vector2(2, 2) shows half as much world, so everything looks twice as big. Pixel-art games often zoom 3 or 4.
  • position_smoothing_enabled and position_smoothing_speed: the camera eases toward the target instead of snapping.
  • limit_left, limit_top, limit_right, limit_bottom: stop the camera at the level edges.
  • drag_horizontal_enabled and friends: a dead zone where the target can move without moving the camera.
  • offset: shift the view, for example to look ahead of the player.
GDSCRIPT
extends Camera2D

func _ready() -> void:
	zoom = Vector2(3, 3)
	position_smoothing_enabled = true
	position_smoothing_speed = 6.0
	limit_left = 0
	limit_top = 0
	limit_right = 2400   # level width in pixels
	limit_bottom = 640

If the player is a child of a Node2D that does not move and the camera is not a child of the player, use a RemoteTransform2D on the player pointing at the camera, or set the camera's global_position each frame.

Smoothing is exponential easing

Smoothing multiplies the remaining distance by a factor each frame. The camera moves quickly when far away and slowly when close, never quite overshooting. You can write the same thing yourself with lerp. Note the frame-rate independent form: use 1 - exp(-speed * delta) instead of a fixed factor, so the camera behaves the same at 30 and 144 FPS.

GDSCRIPT
func _process(delta: float) -> void:
	var t := 1.0 - exp(-8.0 * delta)
	global_position = global_position.lerp(target.global_position, t)
Python Stand-in (Python, not Godot): fixed lerp vs frame-rate independent lerp

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fixed factor ends at a different place depending on frame rate, which is a classic bug on fast monitors. The exponential version agrees across rates (small differences come from rounding the frame count).

Limits and clamping

Limits are a clamp on where the edge of the view may go. The camera center is kept at least half a screen (divided by zoom) away from each limit.

Python Stand-in (Python, not Godot): clamp the camera inside the level

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Screen shake

Shake is a short, decaying random offset. The standard recipe stores a "trauma" value from 0 to 1, adds to it on impact, and shakes by traumaΒ² so small hits are subtle and big hits are violent:

GDSCRIPT
var trauma := 0.0

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

func _process(delta: float) -> void:
	trauma = maxf(trauma - delta * 1.5, 0.0)
	var shake := trauma * trauma
	offset = Vector2(randf_range(-1, 1), randf_range(-1, 1)) * 24.0 * shake
Python Stand-in (Python, not Godot): trauma decays, shake falls off faster

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Parallax backgrounds

Parallax means far things move slower than near things when the camera moves. In Godot 4.3 and later add a Parallax2D node per layer and set scroll_scale (for example 0.2 for distant mountains, 0.6 for trees). scroll_scale = Vector2(1, 1) moves with the world, 0 stays glued to the screen. Set repeat_size to the width of the texture and the layer tiles forever. Older projects use ParallaxBackground plus ParallaxLayer, which still works.

flowchart LR
    C["Camera2D<br/>moves 100 px"] --> L1["Sky<br/>scroll 0.0"]
    C --> L2["Mountains<br/>scroll 0.2"]
    C --> L3["Trees<br/>scroll 0.6"]
    C --> L4["Ground<br/>scroll 1.0"]

Each layer moves by camera movement multiplied by its scroll scale.

Python Stand-in (Python, not Godot): layer offsets for a camera move

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Resolution and stretch

A game must look right on many screen sizes. In Project Settings > Display > Window set the base size (for example 640 by 360), Stretch Mode canvas_items (2D) and Aspect keep (black bars) or expand. For pixel art use stretch mode viewport with integer scaling to keep pixels square.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. How do you make Camera2D follow the player in the simplest way?

    A child node inherits its parent's transform, so a camera child follows automatically.
  2. 2. What does zoom = Vector2(2, 2) do?

    Higher zoom magnifies, so the visible world area shrinks.
  3. 3. Why use 1 - exp(-speed * delta) for smoothing?

    A fixed factor per frame behaves differently at 30 and 144 FPS; the exponential form accounts for delta.
  4. 4. A Parallax2D layer has scroll_scale (0.2, 0.2). How does it move?

    Smaller scroll scale means slower apparent movement, which reads as distance.

Summary

  • Camera2D as a child of the player follows it; tune zoom, smoothing, limits and drag margins.
  • Use the exponential lerp form for frame-rate independent smoothing.
  • Shake with decaying trauma squared.
  • Parallax2D layers with different scroll_scale values create depth.
  • Set stretch mode and aspect in Project Settings so the game scales on every screen.

Next: 3D Scenes, Meshes and Materials