Part of these game systems:
Smooth Camera Follow 2D
Smooth camera follow with offset, dead zone, and look-ahead for 2D games.
How to Use
1
Attach to your Main Camera
2
Drag your player into the Target field
3
Adjust offset and smooth speed
4
Optionally enable bounds to limit camera area
Source Code
C#
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;
}
}