A good platformer camera moves as little as it can and shows what's in front of you. This one ignores hops, aims 110 px ahead of where you're running, and smooths the same at any frame rate.
| Value | |
|---|---|
| Vertical window around the last floor | 170 px up, 50 px down |
| Full jump height | 144 px |
| Look-ahead | 110 px (2.75 tiles) |
| Follow smoothing | half the distance in 0.08 s |
Platform snapping: don't follow the jump
A camera glued to the player scrolls the whole level on every hop. Platform snapping keeps it at the height of the last floor you stood on, and only follows if you go 170 px above it or 50 px below. A full jump rises 144 px, so a hop never moves the screen. Downward the window is small, because below you is where you'll land.
Itay Keren's GDC talk Scroll Back credits Super Mario World with introducing it.
Look-ahead: frame where the player is going
The space that matters is in front of you, so the camera aims 110 px ahead of where you're moving. It only switches sides above 60 px/s, so turning in place doesn't swing the view, and it eases slower than the follow: half the distance in 0.27 s, against 0.08 s.
Camera smoothing that works at any frame rate
lerp(position, target, 0.1) covers 10% of what's left every frame, so the camera gets stiffer the more FPS you have. Use 1 − e^(−k·Δt) as the fraction instead and the distance left after t seconds is the same at any frame rate. Celeste writes the same idea as 1 - 0.01^Δt.
How to implement a platformer camera
- Anchor the height to the last floor or wall slide. In the air, only drag it when the player leaves the window.
- Ease the look-ahead slower than the follow, and flip it only on a real run.
- Smooth by time, not by frame.
- Snap on respawn. Smoothing across half a level after a death is a slow pan nobody asked for.
The code in Godot 4
func _physics_process(delta: float) -> void:
var pos := target.global_position
if Lab.on(&"camera_smart"):
# Look-ahead: sólo cambia de lado cuando el jugador se mueve de verdad.
if absf(target.velocity.x) > 60.0:
_look_target = signf(target.velocity.x) * look_ahead
_look = BP.damp(_look, _look_target, look_sharpness, delta)
# Platform snapping: re-anclar al pisar suelo (o al deslizar por una pared).
if target.is_on_floor() or target.is_wall_sliding:
_anchor_y = pos.y
else:
_anchor_y = clampf(_anchor_y, pos.y - window_down, pos.y + window_up)
var desired := Vector2(pos.x + _look, _anchor_y - vertical_offset)
_focus.x = BP.damp(_focus.x, desired.x, follow_sharpness_x, delta)
_focus.y = BP.damp(_focus.y, desired.y, follow_sharpness_y, delta) ## Suavizado exponencial independiente del framerate.
## `lerp(a, b, 0.1)` por frame cambia de velocidad con los FPS; esto no.
static func damp(from: float, to: float, sharpness: float, delta: float) -> float:
return lerpf(from, to, 1.0 - exp(-sharpness * delta)) Godot's Camera2D has drag margins, smoothing and limits but no look-ahead and nothing that re-anchors on landing, so this script sets its position every physics tick.
Quick answers
How far ahead should a platformer camera look?
About 110 px, roughly 2.75 tiles. Keren's article describes Defender aiming about 25% of the screen width in front of the ship.
Should a platformer camera follow jumps?
Not vertically. Make the window taller than a full jump (170 px against 144 here) and a jump on its own never moves the screen.
Discussion
Comments are hosted on GitHub Discussions — sign in with GitHub to reply.