@@ -4,41 +4,32 @@ public class CameraController : MonoBehaviour
44{
55 public static CameraController Instance ;
66
7- public CameraMode currentMode = CameraMode . FreeLook ;
8-
9- private Camera mainCamera ;
10-
11- public Transform selectedVehicle ;
12-
13- [ Header ( "Free Look Camera Settings" ) ]
7+ [ Header ( "Movement Settings" ) ]
148 public float moveSpeed = 20f ;
15- public float zoomSpeed = 5f ;
16- public float rotateSpeed = 100f ;
17- public float verticalSpeed = 10f ;
9+ public float fastMoveMultiplier = 2f ;
10+ public float smoothTime = 0.1f ;
1811
19- [ Header ( "TPS Camera Settings" ) ]
20- public Transform tpsCameraPosition ;
21- public float heightOffset = 2f ;
22- public float tpsSensitivityX = 2f ;
23- public float tpsSensitivityY = 2f ;
24- public float minVerticalAngle = - 30f ;
25- public float maxVerticalAngle = 40f ;
12+ [ Header ( "Zoom Settings" ) ]
13+ public float zoomSpeed = 5f ;
14+ public float minZoom = 5f ;
15+ public float maxZoom = 50f ;
2616
27- public float currentYaw ;
28- public float currentPitch ;
17+ [ Header ( "Rotation Settings" ) ]
18+ public float rotateSpeed = 100f ;
19+ public float smoothRotation = 5f ;
20+ public float minPitch = - 80f ;
21+ public float maxPitch = 80f ;
2922
23+ [ Header ( "Bounds" ) ]
3024 public Vector2 panLimitX = new Vector2 ( - 50f , 50f ) ;
3125 public Vector2 panLimitZ = new Vector2 ( - 50f , 50f ) ;
3226 public Vector2 panLimitY = new Vector2 ( 5f , 50f ) ;
3327
34- public float minZoom = 5f ;
35- public float maxZoom = 50f ;
36-
37- public Vector3 cameraPosition ;
38-
39- public Vector3 cameraRotation ;
40-
28+ private Vector3 targetPosition ;
29+ private Vector3 velocity = Vector3 . zero ;
4130 private Vector3 lastMousePosition ;
31+ private float currentYaw ;
32+ private float currentPitch ;
4233
4334 private void Awake ( )
4435 {
@@ -54,74 +45,54 @@ private void Awake()
5445
5546 private void Start ( )
5647 {
57- mainCamera = Camera . main ;
58- }
59-
60- private void Update ( )
61- {
62- if ( Input . GetKeyDown ( KeyCode . T ) )
63- {
64- SwitchCameraMode ( ) ;
65- }
48+ targetPosition = transform . position ;
49+
50+ Vector3 eulerAngles = transform . rotation . eulerAngles ;
51+ currentYaw = eulerAngles . y ;
52+ currentPitch = eulerAngles . x ;
53+
54+ if ( currentPitch > 180f )
55+ currentPitch -= 360f ;
6656 }
6757
6858 private void LateUpdate ( )
6959 {
70- if ( currentMode == CameraMode . FreeLook )
71- {
72- HandleMovement ( ) ;
73- HandleZoom ( ) ;
74- HandleRotation ( ) ;
75- }
76- else if ( currentMode == CameraMode . ThirdPerson )
77- {
78- RotateTPSCameraWithMouse ( ) ;
79- }
60+ HandleMovement ( ) ;
61+ HandleZoom ( ) ;
62+ HandleRotation ( ) ;
63+ ApplySmoothMovement ( ) ;
8064 }
8165
82- private void SwitchCameraMode ( )
83- {
84- currentMode = ( currentMode == CameraMode . FreeLook ) ? CameraMode . ThirdPerson : CameraMode . FreeLook ;
85-
86- if ( currentMode == CameraMode . FreeLook )
87- {
88- mainCamera . transform . position = cameraPosition ;
89- mainCamera . transform . rotation = Quaternion . Euler ( cameraRotation ) ;
90- }
91- }
92-
93- #region Free Look Camera
9466 private void HandleMovement ( )
9567 {
9668 Vector3 move = Vector3 . zero ;
69+ float speedMultiplier = Input . GetKey ( KeyCode . LeftShift ) ? fastMoveMultiplier : 1f ;
9770
9871 if ( Input . GetKey ( KeyCode . W ) ) move += transform . forward ;
9972 if ( Input . GetKey ( KeyCode . S ) ) move -= transform . forward ;
10073 if ( Input . GetKey ( KeyCode . D ) ) move += transform . right ;
10174 if ( Input . GetKey ( KeyCode . A ) ) move -= transform . right ;
102-
10375 if ( Input . GetKey ( KeyCode . E ) ) move += Vector3 . up ;
10476 if ( Input . GetKey ( KeyCode . Q ) ) move -= Vector3 . up ;
10577
106- transform . position += move * moveSpeed * Time . deltaTime ;
78+ targetPosition += move * moveSpeed * speedMultiplier * Time . deltaTime ;
10779
108- transform . position = new Vector3 (
109- Mathf . Clamp ( transform . position . x , panLimitX . x , panLimitX . y ) ,
110- Mathf . Clamp ( transform . position . y , panLimitY . x , panLimitY . y ) ,
111- Mathf . Clamp ( transform . position . z , panLimitZ . x , panLimitZ . y )
80+ targetPosition = new Vector3 (
81+ Mathf . Clamp ( targetPosition . x , panLimitX . x , panLimitX . y ) ,
82+ Mathf . Clamp ( targetPosition . y , panLimitY . x , panLimitY . y ) ,
83+ Mathf . Clamp ( targetPosition . z , panLimitZ . x , panLimitZ . y )
11284 ) ;
113-
114- cameraPosition = transform . position ;
11585 }
11686
11787 private void HandleZoom ( )
11888 {
11989 float scroll = Input . GetAxis ( "Mouse ScrollWheel" ) ;
120- Vector3 zoomDirection = transform . forward * scroll * zoomSpeed ;
121-
122- Vector3 newPosition = transform . position + zoomDirection ;
123- newPosition . y = Mathf . Clamp ( newPosition . y , minZoom , maxZoom ) ;
124- transform . position = newPosition ;
90+ if ( Mathf . Abs ( scroll ) > 0.01f )
91+ {
92+ Vector3 zoomDirection = transform . forward * scroll * zoomSpeed ;
93+ targetPosition += zoomDirection ;
94+ targetPosition . y = Mathf . Clamp ( targetPosition . y , minZoom , maxZoom ) ;
95+ }
12596 }
12697
12798 private void HandleRotation ( )
@@ -130,59 +101,25 @@ private void HandleRotation()
130101 {
131102 lastMousePosition = Input . mousePosition ;
132103 }
104+
133105 if ( Input . GetMouseButton ( 1 ) )
134106 {
135107 Vector3 delta = Input . mousePosition - lastMousePosition ;
136- float rotationX = delta . x * rotateSpeed * Time . deltaTime ;
137- float rotationY = - delta . y * rotateSpeed * Time . deltaTime ;
138-
139- transform . Rotate ( Vector3 . up , rotationX , Space . World ) ;
140- transform . Rotate ( Vector3 . right , rotationY , Space . Self ) ;
141-
108+
109+ currentYaw += delta . x * rotateSpeed * Time . deltaTime ;
110+ currentPitch -= delta . y * rotateSpeed * Time . deltaTime ;
111+
112+ currentPitch = Mathf . Clamp ( currentPitch , minPitch , maxPitch ) ;
113+
142114 lastMousePosition = Input . mousePosition ;
143-
144- cameraRotation = transform . eulerAngles ;
145115 }
146116 }
147- #endregion
148-
149- #region Third Person Camera
150117
151- private void RotateTPSCameraWithMouse ( )
118+ private void ApplySmoothMovement ( )
152119 {
153- float mouseX = Input . GetAxis ( "Mouse X" ) * tpsSensitivityX ;
154- float mouseY = Input . GetAxis ( "Mouse Y" ) * tpsSensitivityY ;
155-
156- currentYaw += mouseX ;
157- currentPitch -= mouseY ;
158- currentPitch = Mathf . Clamp ( currentPitch , minVerticalAngle , maxVerticalAngle ) ;
159-
160- if ( selectedVehicle != null )
161- {
162- Vector3 offset = new Vector3 ( 0f , heightOffset , - 15f ) ;
163- Quaternion rotation = Quaternion . Euler ( currentPitch , currentYaw , 0f ) ;
164- Vector3 desiredPosition = selectedVehicle . position + rotation * offset ;
165-
166- // Add a smooth shake effect
167- Vector3 shakeOffset = new Vector3 (
168- Mathf . PerlinNoise ( Time . time * 2f , 0f ) - 0.5f ,
169- Mathf . PerlinNoise ( 0f , Time . time * 2f ) - 0.5f ,
170- Mathf . PerlinNoise ( Time . time * 2f , Time . time * 2f ) - 0.5f
171- ) * 0.2f ; // Adjust shake intensity here
172-
173- tpsCameraPosition . position = Vector3 . Lerp ( tpsCameraPosition . position , desiredPosition + shakeOffset , Time . deltaTime * moveSpeed ) ;
174- mainCamera . transform . position = tpsCameraPosition . position ;
175- mainCamera . transform . rotation = Quaternion . LookRotation ( ( selectedVehicle . position + Vector3 . up * heightOffset ) - mainCamera . transform . position ) ;
176-
177- // Gradually reduce shake effect over time
178- shakeOffset *= 0.9f ;
179- }
120+ transform . position = Vector3 . SmoothDamp ( transform . position , targetPosition , ref velocity , smoothTime ) ;
121+
122+ Quaternion targetRotation = Quaternion . Euler ( currentPitch , currentYaw , 0f ) ;
123+ transform . rotation = Quaternion . Slerp ( transform . rotation , targetRotation , Time . deltaTime * smoothRotation ) ;
180124 }
181- #endregion
182125}
183-
184- public enum CameraMode
185- {
186- FreeLook ,
187- ThirdPerson
188- }
0 commit comments