1+ using UnityEngine ;
2+
3+ public class CameraController : MonoBehaviour
4+ {
5+ [ Header ( "Target Settings" ) ]
6+ public Transform target ;
7+ public Vector3 offset = new Vector3 ( 0 , 5 , - 10 ) ;
8+
9+ [ Header ( "Smooth Settings" ) ]
10+ public float smoothSpeed = 5f ;
11+ public float rotationSpeed = 2f ;
12+
13+ [ Header ( "Boundaries" ) ]
14+ public float minY = 2f ;
15+ public float maxY = 10f ;
16+ public float minX = - 10f ;
17+ public float maxX = 10f ;
18+
19+ private void LateUpdate ( )
20+ {
21+ if ( target == null ) return ;
22+
23+ // Calculate desired position
24+ Vector3 desiredPosition = target . position + offset ;
25+
26+ // Clamp position within boundaries
27+ desiredPosition . y = Mathf . Clamp ( desiredPosition . y , minY , maxY ) ;
28+ desiredPosition . x = Mathf . Clamp ( desiredPosition . x , minX , maxX ) ;
29+
30+ // Smoothly move camera
31+ Vector3 smoothedPosition = Vector3 . Lerp ( transform . position , desiredPosition , smoothSpeed * Time . deltaTime ) ;
32+ transform . position = smoothedPosition ;
33+
34+ // Look at target
35+ Vector3 targetPosition = new Vector3 ( target . position . x , target . position . y , target . position . z ) ;
36+ Quaternion targetRotation = Quaternion . LookRotation ( targetPosition - transform . position ) ;
37+ transform . rotation = Quaternion . Slerp ( transform . rotation , targetRotation , rotationSpeed * Time . deltaTime ) ;
38+ }
39+ }
0 commit comments