Phaser Camera Follow, Explained: Lerp, Deadzone and roundPixels


What camera follow actually does

A follow camera keeps a moving target near the middle of the screen. In Phaser, startFollow(target, [roundPixels], [lerpX], [lerpY], [offsetX], [offsetY]) tells the camera to track a Game Object and adjust its scroll so the target stays centered. Three of its settings, lerp, deadzone and roundPixels, control how that motion feels.

This post is based on the Phaser Camera docs, which show a Phaser v4.1.0 header, and the matching Camera.js source. It explains each knob using only what those pages state.

How startFollow works

Call startFollow on a scene camera and pass the Game Object to track. The camera then adjusts its scroll to keep that target centered. Only target is required. The other arguments refine the motion.

startFollow(target, [roundPixels], [lerpX], [lerpY], [offsetX], [offsetY])
  • target: the Game Object to follow. The only required argument.
  • roundPixels: pass true to round the position to whole pixels, which the docs suggest can reduce jitter.
  • lerpX, lerpY: how quickly the camera tracks on each axis.
  • offsetX, offsetY: an offset from the target. The docs say these values are subtracted from the target position during the camera update.

Knob 1: lerp, smoothing the motion

Lerp controls how quickly the camera catches up to the target. The docs say to keep the value between 0 and 1, and that a value of 0 disables tracking on that axis. Values closer to 1 track faster, and the default of 1 snaps the camera straight to the target.

The docs give 0.1 as an example of a low value that smooths motion. It is not a recommended setting. You can set lerp in startFollow or with setLerp([x], [y]), which takes X and Y separately and can be called again at runtime.

Knob 2: deadzone, a no-scroll rectangle

setDeadzone([width], [height]) defines a rectangle that the docs describe as re-positioned every frame so it stays centered on the camera’s midpoint. While the target is inside it, the camera does not scroll. Once the target leaves the rectangle, the camera scrolls to follow.

Three details matter:

  1. It only applies while following. Without startFollow, the deadzone has no effect.
  2. Lerp still applies. The docs say the lerp values also apply with a deadzone. The deadzone decides when the camera scrolls, and lerp decides how smoothly it does.
  3. Calling it with no arguments resets it.

The docs also note that you can check whether an object is inside the rectangle with Rectangle.contains.

Knob 3: roundPixels, for jitter

The docs say slight jitter is “probably” caused by sub-pixel rendering of the target’s position. Passing roundPixels as true addresses that. The docs also warn that it can still break when the camera zoom is not an integer, so keep zoom at whole numbers.

Putting it together: a minimal follow scene

The sketch below shows the three knobs in one create() method. It is written from the documented API. It was not run.

// Inside create(). `player` is the Game Object to track.
const cam = this.cameras.main;

// Start following: target, roundPixels, lerpX, lerpY.
// Lerp must be between 0 and 1; 0 disables tracking on that axis.
cam.startFollow(player, true, 0.1, 0.1);

// Deadzone: a rectangle centered on the camera.
// While the player stays inside it, the camera does not scroll.
cam.setDeadzone(120, 80);

// Keep zoom at an integer (for example, cam.zoom = 2) so roundPixels keeps working.

To change the lerp while the game runs, call cam.setLerp(x, y).

The Camera page also documents setFollowOffset([x], [y]), which sets the camera’s offset from its follow target, and stopFollow(), which stops following a target set with startFollow. Check the docs page for the full details.

FAQ

What lerp value should I use?

The docs do not name a recommended number. They give 0.1 as an example of a low value that smooths motion. Start from the documented behaviour (1 snaps, lower values track more slowly, 0 disables tracking), then adjust it while playing.

Why is my camera still jittery even with roundPixels on?

The docs attribute jitter to sub-pixel rendering, and roundPixels is meant to address it. The docs also say it can still break with a non-integer zoom. If the camera still shimmers with roundPixels on, check the zoom first.

Do lerp and deadzone work together?

Yes. The docs say lerp values also apply with a deadzone. The deadzone sets when the camera scrolls, and lerp sets how smoothly it moves once it does.

What you learned

  • startFollow attaches the camera to a Game Object and keeps it centered.
  • Lerp values sit between 0 and 1. The default of 1 snaps, and 0 disables tracking on that axis.
  • setDeadzone creates a rectangle around the camera’s midpoint. The camera does not scroll while the target is inside it, and lerp still applies.
  • roundPixels targets sub-pixel jitter. Keep zoom at integers, because a non-integer zoom can still break it.

For more, read the Phaser Camera docs and the v4.1.0 Camera.js source.