Camera Follow (with lerp smoothing)
Camera Follow (with lerp smoothing)¶
function updateCamera(camera, target, smoothing, dt):
desiredPosition = target.position - (screenWidth/2, screenHeight/2)
camera.position = lerpVector(camera.position, desiredPosition, smoothing * dt)
Camera follow via Linear Interpolation (Lerp) treats the camera's position as chasing a moving target: each frame it moves a fraction of the remaining distance toward where it wants to be, rather than snapping instantly. Because smoothing * dt is typically small (e.g. a smoothing value around 2–5), the camera closes the gap gradually — this produces the natural "catching up" ease-out motion common in platformers and top-down games. Note this is technically an approximation of exponential smoothing; for frame-rate independence at very high or variable dt, 1 - exp(-smoothing * dt) is more mathematically correct, but plain lerp is a common, visually-fine simplification.