-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCinematicCameraController.cs
More file actions
314 lines (255 loc) · 10.2 KB
/
CinematicCameraController.cs
File metadata and controls
314 lines (255 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using UnityEngine;
using System.Collections;
public class CinematicCameraController : MonoBehaviour
{
[Header("Transforms")]
[SerializeField] private Transform homeTransform;
[SerializeField] private Transform targetTransform;
[SerializeField] private float homeTransitionDuration = 2f;
[Header("Orbit Settings")]
[SerializeField] private float orbitDistance = 15f;
[SerializeField] private float minOrbitDistance = 3f;
[SerializeField] private float maxOrbitDistance = 50f;
[Header("Input Sensitivity")]
[SerializeField] private float orbitSensitivity = 2f;
[SerializeField] private float panSensitivity = 1f;
[SerializeField] private float zoomSensitivity = 3f;
[Header("Smoothing")]
[SerializeField] private float orbitSmoothing = 8f;
[SerializeField] private float panSmoothing = 5f;
[SerializeField] private float zoomSmoothing = 10f;
[Header("Auto Rotation")]
[SerializeField] private bool enableAutoRotation = true;
[SerializeField] private float autoRotationSpeed = 5f;
[SerializeField] private float idleTimeBeforeAutoRotation = 4f;
// Camera state
private float horizontalAngle = 0f;
private float verticalAngle = 20f;
private Vector3 currentOrbitCenter;
private float currentOrbitDistance;
// Input tracking
private bool isRightMouseDown = false;
private float lastInputTime;
// Smoothing
private float targetHorizontalAngle;
private float targetVerticalAngle;
private float targetOrbitDistance;
private Vector3 targetOrbitCenter;
// State management
private bool isTransitioning = false;
private bool isAtHome = false;
void Start()
{
InitializeCamera();
}
void Update()
{
HandleInput();
UpdateCameraPosition();
HandleAutoRotation();
}
private void InitializeCamera()
{
// Always start at home
if (homeTransform != null)
{
transform.position = homeTransform.position;
transform.rotation = homeTransform.rotation;
isAtHome = true;
}
// Set orbit center to target transform at HOME height
if (targetTransform != null && homeTransform != null)
{
Vector3 targetPos = targetTransform.position;
Vector3 homePos = homeTransform.position;
currentOrbitCenter = new Vector3(targetPos.x, homePos.y, targetPos.z);
targetOrbitCenter = currentOrbitCenter;
}
// Set initial distance
currentOrbitDistance = orbitDistance;
targetOrbitDistance = orbitDistance;
targetHorizontalAngle = horizontalAngle;
targetVerticalAngle = verticalAngle;
lastInputTime = Time.time;
}
private void HandleInput()
{
// ESC to return home
if (Input.GetKeyDown(KeyCode.Escape))
{
ReturnHome();
return;
}
// Update orbit center from target transform at HOME height
if (targetTransform != null && homeTransform != null)
{
Vector3 targetPos = targetTransform.position;
Vector3 homePos = homeTransform.position;
targetOrbitCenter = new Vector3(targetPos.x, homePos.y, targetPos.z);
}
// Right mouse button for panning
if (Input.GetMouseButtonDown(1))
{
isRightMouseDown = true;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else if (Input.GetMouseButtonUp(1))
{
isRightMouseDown = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
bool hasInput = false;
// Left mouse drag for orbit rotation
if (Input.GetMouseButton(0) && !isRightMouseDown && !isTransitioning)
{
ExitHomeMode();
float mouseX = Input.GetAxis("Mouse X") * orbitSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * orbitSensitivity;
targetHorizontalAngle += mouseX;
targetVerticalAngle -= mouseY;
targetVerticalAngle = Mathf.Clamp(targetVerticalAngle, -80f, 80f);
hasInput = true;
}
// Right mouse drag for panning (just moves around target)
if (isRightMouseDown && !isTransitioning)
{
ExitHomeMode();
float mouseX = Input.GetAxis("Mouse X") * panSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * panSensitivity;
// Simple pan - just adjust the angles like orbiting
targetHorizontalAngle += mouseX * 0.5f;
targetVerticalAngle -= mouseY * 0.5f;
targetVerticalAngle = Mathf.Clamp(targetVerticalAngle, -80f, 80f);
hasInput = true;
}
// Mouse scroll for zoom
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (Mathf.Abs(scroll) > 0.01f && !isTransitioning)
{
ExitHomeMode();
targetOrbitDistance -= scroll * zoomSensitivity;
targetOrbitDistance = Mathf.Clamp(targetOrbitDistance, minOrbitDistance, maxOrbitDistance);
hasInput = true;
}
// Update input timer for auto rotation
if (hasInput)
{
lastInputTime = Time.time;
}
}
private void ExitHomeMode()
{
if (isAtHome)
{
isAtHome = false;
// Use target position but keep HOME height
if (targetTransform != null && homeTransform != null)
{
Vector3 targetPos = targetTransform.position;
Vector3 homePos = homeTransform.position;
// Target position but at HOME height
currentOrbitCenter = new Vector3(targetPos.x, homePos.y, targetPos.z);
targetOrbitCenter = currentOrbitCenter;
}
// Start with basic angles - no calculations
horizontalAngle = 0f;
verticalAngle = 0f; // Keep level with home height
currentOrbitDistance = orbitDistance;
targetHorizontalAngle = horizontalAngle;
targetVerticalAngle = verticalAngle;
targetOrbitDistance = currentOrbitDistance;
}
}
private void UpdateCameraPosition()
{
if (isTransitioning || isAtHome) return;
// Smooth angle transitions
horizontalAngle = Mathf.LerpAngle(horizontalAngle, targetHorizontalAngle, Time.deltaTime * orbitSmoothing);
verticalAngle = Mathf.Lerp(verticalAngle, targetVerticalAngle, Time.deltaTime * orbitSmoothing);
// Smooth distance transition
currentOrbitDistance = Mathf.Lerp(currentOrbitDistance, targetOrbitDistance, Time.deltaTime * zoomSmoothing);
// Smooth center transition - follow target but keep HOME height
if (targetTransform != null && homeTransform != null)
{
Vector3 targetPos = targetTransform.position;
Vector3 homePos = homeTransform.position;
Vector3 targetAtHomeHeight = new Vector3(targetPos.x, homePos.y, targetPos.z);
currentOrbitCenter = Vector3.Lerp(currentOrbitCenter, targetAtHomeHeight, Time.deltaTime * panSmoothing);
}
// Calculate camera position around target
Quaternion rotation = Quaternion.Euler(verticalAngle, horizontalAngle, 0f);
Vector3 direction = rotation * Vector3.back;
Vector3 targetPosition = currentOrbitCenter + direction * currentOrbitDistance;
// Apply position and look at target
transform.position = targetPosition;
transform.LookAt(currentOrbitCenter, Vector3.up);
}
private void HandleAutoRotation()
{
if (!enableAutoRotation || isTransitioning || isAtHome) return;
if (isRightMouseDown) return;
float timeSinceInput = Time.time - lastInputTime;
if (timeSinceInput > idleTimeBeforeAutoRotation)
{
targetHorizontalAngle += autoRotationSpeed * Time.deltaTime;
}
}
public void ReturnHome()
{
if (homeTransform != null && !isTransitioning)
{
StartCoroutine(SmoothReturnHome());
}
}
private IEnumerator SmoothReturnHome()
{
isTransitioning = true;
Vector3 startPos = transform.position;
Quaternion startRot = transform.rotation;
float elapsed = 0f;
while (elapsed < homeTransitionDuration)
{
elapsed += Time.deltaTime;
float t = elapsed / homeTransitionDuration;
float smoothT = Mathf.SmoothStep(0f, 1f, t);
transform.position = Vector3.Lerp(startPos, homeTransform.position, smoothT);
transform.rotation = Quaternion.Slerp(startRot, homeTransform.rotation, smoothT);
yield return null;
}
transform.position = homeTransform.position;
transform.rotation = homeTransform.rotation;
isAtHome = true;
isTransitioning = false;
lastInputTime = Time.time;
}
// Public properties for external access
public bool IsTransitioning => isTransitioning;
public bool IsAtHome => isAtHome;
void OnDrawGizmosSelected()
{
// Draw target
if (targetTransform != null)
{
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(targetTransform.position, 0.5f);
// Draw orbit sphere around target
Gizmos.color = new Color(0f, 1f, 1f, 0.3f);
Gizmos.DrawWireSphere(targetTransform.position, orbitDistance);
// Draw camera to target line
if (!isAtHome)
{
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, targetTransform.position);
}
}
// Draw home transform
if (homeTransform != null)
{
Gizmos.color = Color.green;
Gizmos.DrawWireCube(homeTransform.position, Vector3.one);
Gizmos.DrawRay(homeTransform.position, homeTransform.forward * 3f);
}
}
}