using UnityEngine; // The camera is not outside the game public class ClampedFollowCamera : MonoBehaviour { [Header("References")] [SerializeField] private Transform player; [SerializeField] private Transform startPoint; [SerializeField] private Transform finishPoint; [Header("Settings")] [Tooltip("How quickly the camera catches up")] [SerializeField] private float smoothSpeed = 5f; private float camHalfWidth; void Start() { // Get half the camera width based on orthographic size camHalfWidth = Camera.main.orthographicSize * Camera.main.aspect; } void LateUpdate() { if (player == null || startPoint == null || finishPoint == null) return; // Clamp camera so its edges don’t go past start/finish float minX = startPoint.position.x + camHalfWidth; float maxX = finishPoint.position.x - camHalfWidth; float targetX = Mathf.Clamp(player.position.x, minX, maxX); Vector3 targetPosition = new Vector3(targetX, transform.position.y, transform.position.z); transform.position = Vector3.Lerp(transform.position, targetPosition, smoothSpeed * Time.deltaTime); } }