Smooth Camera Follow 2D
Smooth camera follow with offset, dead zone, and look-ahead for 2D games.
No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked
What it adds to your GameObject
A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 8 serialized fields with their defaults.
When to use it
Ideal for 2D platformers, side-scrollers, and top-down 2D games that need a camera smoothly tracking the player. Use this when you want a polished camera that looks ahead of the player's movement and stays within defined level boundaries. Pairs naturally with any 2D player controller.
Setup
- Attach to your Main Camera
- Drag your player into the Target field
- Adjust offset and smooth speed
- Optionally enable bounds to limit camera area
Features
- Smooth Lerp-based camera following with adjustable speed
- Configurable offset for custom framing
- Look-ahead in movement direction for better visibility
- Optional camera bounds to clamp within level edges
- Null-safe target check prevents errors if player is destroyed
- Works in LateUpdate for jitter-free rendering
Inspector fields
Every field Smooth Camera Follow 2D exposes in the Inspector, with the default it starts at.
| Field | Type | Default | Group |
|---|---|---|---|
Targettarget | Transform | None | Target |
Offsetoffset | Vector3 | (0, 1, -10) | Follow Settings |
Smooth SpeedsmoothSpeed | float | 8 | Follow Settings |
Look Ahead DistancelookAheadDistance | float | 2 | Look Ahead |
Look Ahead SpeedlookAheadSpeed | float | 4 | Look Ahead |
Use BoundsuseBounds | bool | false | Bounds (Optional) |
Min BoundsminBounds | Vector2 | (0, 0) | Bounds (Optional) |
Max BoundsmaxBounds | Vector2 | (0, 0) | Bounds (Optional) |
Requirements
- Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.
Common mistakes
Forgetting to set the Z offset to -10 (or your camera's Z position) will place the camera at the same depth as sprites, making nothing visible. If useBounds is enabled but minBounds/maxBounds are left at zero, the camera will be locked to the origin. Make sure the Target field is assigned — the script silently returns if target is null.
Source code
CameraFollow2D.cs, 45 lines, MIT licensed. Paste it into a new C# file of the same name.
using UnityEngine;
public class CameraFollow2D : MonoBehaviour
{
[Header("Target")]
[SerializeField] private Transform target;
[Header("Follow Settings")]
[SerializeField] private Vector3 offset = new Vector3(0f, 1f, -10f);
[SerializeField] private float smoothSpeed = 8f;
[Header("Look Ahead")]
[SerializeField] private float lookAheadDistance = 2f;
[SerializeField] private float lookAheadSpeed = 4f;
[Header("Bounds (Optional)")]
[SerializeField] private bool useBounds;
[SerializeField] private Vector2 minBounds;
[SerializeField] private Vector2 maxBounds;
private float currentLookAhead;
private void LateUpdate()
{
if (target == null) return;
// Look ahead in movement direction
float targetLookAhead = Input.GetAxisRaw("Horizontal") * lookAheadDistance;
currentLookAhead = Mathf.Lerp(currentLookAhead, targetLookAhead, Time.deltaTime * lookAheadSpeed);
Vector3 desiredPosition = target.position + offset;
desiredPosition.x += currentLookAhead;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
// Clamp to bounds
if (useBounds)
{
smoothedPosition.x = Mathf.Clamp(smoothedPosition.x, minBounds.x, maxBounds.x);
smoothedPosition.y = Mathf.Clamp(smoothedPosition.y, minBounds.y, maxBounds.y);
}
transform.position = smoothedPosition;
}
}