Skip to content

Camera Follow (with deadzone)

Camera Follow (with deadzone)

function updateCameraDeadzone(camera, target, deadzone):
    // deadzone: a rectangle centered on screen where target can move freely
    screenPos = target.position - camera.position

    if screenPos.x < deadzone.left:
        camera.x -= (deadzone.left - screenPos.x)
    else if screenPos.x > deadzone.right:
        camera.x += (screenPos.x - deadzone.right)

    if screenPos.y < deadzone.top:
        camera.y -= (deadzone.top - screenPos.y)
    else if screenPos.y > deadzone.bottom:
        camera.y += (screenPos.y - deadzone.bottom)

Deadzone following avoids the camera moving on every tiny player wiggle — it only moves when the target would exit a fixed rectangle on screen, and even then moves just enough to keep the target at the edge of the deadzone rather than the center. This is standard in platformers (so jumping doesn't constantly nudge the camera vertically).