-
Notifications
You must be signed in to change notification settings - Fork 3
/
ThirdPersonCamera.cs
569 lines (476 loc) · 14 KB
/
ThirdPersonCamera.cs
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public enum OrbitPosition
{
Top,
Middle,
Bottom
}
#region Helper Classes
[Serializable]
public class OrbitRing
{
public float radius = 3f;
public float height = 2.5f;
public Color color = Color.red;
public OrbitRing(float radius, float height, Color color)
{
this.radius = radius;
this.height = height;
this.color = color;
}
public OrbitRing(float radius, float height)
{
this.radius = radius;
this.height = height;
this.color = Color.green;
}
public float GetBorderDistanceToReference()
{
return Mathf.Sqrt((radius * radius) + (height * height));
}
}
[Serializable]
public class ZoomOutOnMotionEffect
{
public bool enabled = true;
public float startSpeed = 10f;
public float capSpeed = 15f;
public float startDistanceRatio = 0f;
public float capDistanceRatio = 0.3f;
public float GetDistanceIncreaseForSpeed(float speed)
{
if (enabled && speed > startSpeed)
{
float speedRatio = Mathf.InverseLerp(startSpeed, capSpeed, speed);
float distanceIncrease = 1 + Mathf.Lerp(startDistanceRatio, capDistanceRatio, speedRatio);
return distanceIncrease;
}
else
{
return 1f;
}
}
}
[Serializable]
public class MotionShakeEffect
{
public bool enabled = true;
public float startSpeed = 10f;
public float capSpeed = 15f;
[Header("Vertical")]
public float verticalStartIntensity = 0.02f;
public float verticalCapIntensity = 0.05f;
public float verticalSpeed = 15f;
[Range(0, 2)] public float verticalPhase = 0.5f;
[Header("Horizontal")]
public float horizontalStartIntensity = 0.03f;
public float horizontalCapIntensity = 0.07f;
public float horizontalSpeed = 7.5f;
[Range(0, 2)] public float horizontalPhase = 0f;
private float phase = 0f;
private bool running = false;
public Vector3 Update(float speed, float deltaTime)
{
if (speed >= startSpeed)
{
if (!running)
{
Start();
}
phase += deltaTime;
float speedRatio = Mathf.InverseLerp(startSpeed, capSpeed, speed);
float verticalIntensity = Mathf.Lerp(verticalStartIntensity, verticalCapIntensity, speedRatio);
float horizontalIntensity = Mathf.Lerp(horizontalStartIntensity, horizontalCapIntensity, speedRatio);
float horizontal = Mathf.Sin((Time.time * horizontalSpeed) + (horizontalPhase * Mathf.PI)) * horizontalIntensity;
float vertical = Mathf.Sin((Time.time * verticalSpeed) + (verticalPhase * Mathf.PI)) * verticalIntensity;
return new Vector3(horizontal, vertical, 0f);
}
else
{
Stop();
return Vector3.zero;
}
}
private void Start()
{
phase = 0f;
running = true;
}
private void Stop()
{
running = false;
}
}
#endregion
[ExecuteInEditMode]
public class ThirdPersonCamera : MonoBehaviour
{
#region Inspector Settings
[Header("Editor Settings")]
[SerializeField] bool showGizmos = true;
[SerializeField] bool editorPreview = true;
[Header("Targets")]
[SerializeField] GameObject follow = null;
[SerializeField] GameObject lookAt = null;
[Header("Orbits")]
[SerializeField] OrbitRing topRing = new OrbitRing(2f, 1.4f, Color.red);
[SerializeField] OrbitRing middleRing = new OrbitRing(5f, 3f, Color.red);
[SerializeField] OrbitRing bottomRing = new OrbitRing(1f, -1f, Color.red);
[Header("Positioning")]
[SerializeField] bool lockHeight = false;
[SerializeField][ShowIf("lockHeight")] float fixedHeight = .5f;
[SerializeField] bool lockTranslation = false;
[SerializeField][Range(0f, 360f)][ShowIf("lockTranslation")] float fixedTranslation = 0f;
[SerializeField] bool avoidClipping = true;
[ShowIf(nameof(avoidClipping))][SerializeField] LayerMask avoidClippingLayerMask = 0;
[ShowIf(nameof(avoidClipping))][SerializeField] float clipDistance = 5f;
[ShowIf(nameof(avoidClipping))][SerializeField] float clippingOffset = 0f;
[SerializeField][Range(-180, 180)] float horizontalTilt = 0f;
[SerializeField] float horizontalOffset = 0f;
[SerializeField][Range(-180, 180)] float verticalTilt = 0f;
[SerializeField] float verticalOffset = 0f;
[SerializeField] bool useTargetNormal = true;
[Header("Controls")]
[SerializeField] bool captureCursor = false;
[Header("X axis")]
[SerializeField] string horizontalAxis = "Mouse X";
[SerializeField] float horizontalSensitivity = 1f;
[SerializeField] bool invertX = false;
[Header("Y axis")]
[SerializeField] string verticalAxis = "Mouse Y";
[SerializeField] float verticalSensitivity = 0.8f;
[SerializeField] bool invertY = true;
[Header("Effects")]
[SerializeField] ZoomOutOnMotionEffect zoomOutOnMotion = new ZoomOutOnMotionEffect();
[SerializeField] MotionShakeEffect motionShake = new MotionShakeEffect();
#endregion
#region Private Variables
private float cameraTranslation = 0f;
private float verticalMultiplier = 10f;
private float referenceHeight = 0f;
private float referenceDistance;
private float noClippingHeight;
private float noClippingDistance;
private OrbitRing cameraRing = null;
private Vector3 up;
private Vector3 right;
private Vector3 forward;
#endregion
// ===================== Lifecycle ===================== //
#region Lifecycle Methods
void Start()
{
referenceHeight = middleRing.height;
}
void Update()
{
if ((Application.isPlaying || editorPreview) && Time.timeScale > 0)
{
if (captureCursor && Application.isPlaying)
{
Cursor.lockState = CursorLockMode.Locked;
}
SetNormalVectors();
SetPosition();
SetRotation();
ApplyEffects();
}
}
private void OnDrawGizmos()
{
if (follow != null && showGizmos)
{
DrawRing(topRing);
DrawRing(middleRing);
DrawRing(bottomRing);
}
}
#endregion
// ===================== Update steps ===================== //
#region Update Steps
private void SetNormalVectors()
{
up = useTargetNormal ? follow.transform.up : Vector3.up;
right = Vector3.Cross(up, Vector3.right);
forward = Vector3.Cross(up, right);
}
private void SetPosition()
{
ReadInputs();
referenceDistance = 0f;
cameraRing = GetCameraRing();
referenceHeight = cameraRing.height;
float distance = cameraRing.GetBorderDistanceToReference();
referenceDistance = Mathf.Sqrt((distance * distance) - (referenceHeight * referenceHeight));
referenceDistance = ApplyZoomOutOnMotion(referenceDistance);
if (avoidClipping)
{
CorrectClipping(Mathf.Min(distance, clipDistance));
}
Vector3 heightVector = up * (avoidClipping ? noClippingHeight : referenceHeight);
Vector3 distanceVector = -forward * (avoidClipping ? noClippingDistance : referenceDistance);
transform.position = follow.transform.position + heightVector + distanceVector;
transform.RotateAround(follow.transform.position, up, cameraTranslation);
}
private void SetRotation()
{
LookAt(up, lookAt.transform);
Vector3 verticalAngles = forward * verticalTilt;
Vector3 horizontalAngles = up * horizontalTilt;
Vector3 eulerRotation = verticalAngles + horizontalAngles;
transform.Rotate(eulerRotation.x, eulerRotation.y, eulerRotation.z);
ApplyPositionOffset();
}
private void ApplyEffects()
{
if (motionShake.enabled)
{
ApplyMotionShake();
}
}
#endregion
// ===================== Input ===================== //
#region Input Methods
private void ReadInputs()
{
if (lockHeight)
{
referenceHeight = fixedHeight;
}
else if (Application.isPlaying)
{
referenceHeight += Input.GetAxis(verticalAxis) * verticalSensitivity * (invertY ? -1 : 1);
}
if (lockTranslation)
{
cameraTranslation = fixedTranslation;
}
else if (Application.isPlaying)
{
cameraTranslation += Input.GetAxis(horizontalAxis) * verticalMultiplier * horizontalSensitivity * (invertX ? -1 : 1);
if (cameraTranslation > 360f)
{
cameraTranslation -= 360f;
}
else if (cameraTranslation < 0f)
{
cameraTranslation += 360f;
}
}
}
#endregion
// ===================== Positioning ===================== //
#region Positioning Methods
private OrbitRing GetCameraRing()
{
if (referenceHeight >= topRing.height)
{
return new OrbitRing(topRing.radius, topRing.height);
}
else if (referenceHeight >= middleRing.height)
{
float radius = EaseLerpRingRadius(middleRing, topRing);
return new OrbitRing(radius, referenceHeight);
}
else if (referenceHeight >= bottomRing.height)
{
float radius = EaseLerpRingRadius(bottomRing, middleRing);
return new OrbitRing(radius, referenceHeight);
}
else
{
return new OrbitRing(bottomRing.radius, bottomRing.height);
}
}
private void CorrectClipping(float raycastDistance)
{
Ray ray = new Ray(follow.transform.position, (transform.position - follow.transform.position).normalized);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, raycastDistance, avoidClippingLayerMask, QueryTriggerInteraction.Ignore))
{
float safeDistance = hit.distance - clippingOffset;
float sinAngl = referenceHeight / raycastDistance;
float cosAngl = referenceDistance / raycastDistance;
noClippingHeight = safeDistance * sinAngl;
noClippingDistance = safeDistance * cosAngl;
}
else
{
noClippingHeight = referenceHeight;
noClippingDistance = referenceDistance;
}
}
private void ApplyPositionOffset()
{
transform.position = transform.position + (transform.right * horizontalOffset) + (transform.up * verticalOffset);
}
#endregion
// ===================== Rotation ===================== //
#region Rotation Methods
private void LookAt(Vector3 normal, Transform lookAt)
{
Vector3 targetDirection = (lookAt.position - transform.position).normalized;
transform.localRotation = Quaternion.LookRotation(targetDirection, normal);
}
#endregion
// ===================== Effects ===================== //
#region Effects Methods
private float ApplyZoomOutOnMotion(float distance)
{
Rigidbody rb = follow.GetComponent<Rigidbody>();
if (rb != null)
{
float speed = follow.GetComponent<Rigidbody>().velocity.magnitude;
float distanceIncrease = zoomOutOnMotion.GetDistanceIncreaseForSpeed(speed);
return distanceIncrease * distance;
}
else
{
return distance;
}
}
private void ApplyMotionShake()
{
Rigidbody rb = follow.GetComponent<Rigidbody>();
if (rb == null)
{
return;
}
float speed = follow.GetComponent<Rigidbody>().velocity.magnitude;
Vector3 shake = motionShake.Update(speed, Time.deltaTime);
Vector3 relativeShake = transform.right * shake.x + transform.up * shake.y;
transform.position += relativeShake;
}
#endregion
// ===================== Utils ===================== //
#region Utils Methods
private float EaseLerpRingRadius(OrbitRing r1, OrbitRing r2)
{
float lerpState = Mathf.InverseLerp(r1.height, r2.height, referenceHeight);
if (r1.radius > r2.radius)
{
lerpState = lerpState * lerpState;
}
else
{
lerpState = Mathf.Sqrt(lerpState);
}
float radius = Mathf.Lerp(r1.radius, r2.radius, lerpState);
return radius;
}
private void DrawRing(OrbitRing ring)
{
#if UNITY_EDITOR
Handles.color = ring.color;
Vector3 position = follow.transform.position + (up * ring.height);
Handles.DrawWireDisc(position, up, ring.radius);
#endif
}
#endregion
// ===================== Setters ===================== //
#region Setters Methods
public void SetFollow(GameObject follow)
{
this.follow = follow;
}
public void SetLookAt(GameObject lookAt)
{
this.lookAt = lookAt;
}
public void SetOrbitRing(OrbitPosition position, OrbitRing orbit)
{
if (position == OrbitPosition.Top)
{
topRing = orbit;
}
else if (position == OrbitPosition.Middle)
{
middleRing = orbit;
}
else if (position == OrbitPosition.Bottom)
{
bottomRing = orbit;
}
}
public void SetLockHeight(bool lockHeight)
{
this.lockHeight = lockHeight;
}
public void SetLockTranslation(bool lockTranslation)
{
this.lockTranslation = lockTranslation;
}
public void SetAvoidClipping(bool avoidClipping)
{
this.avoidClipping = avoidClipping;
}
public void SetClipDistance(float clipDistance)
{
this.clipDistance = clipDistance;
}
public void SetClippingOffset(float clippingOffset)
{
this.clippingOffset = clippingOffset;
}
public void SetHorizontalTilt(float horizontalTilt)
{
this.horizontalTilt = horizontalTilt;
}
public void SetHorizontalOffset(float horizontalOffset)
{
this.horizontalOffset = horizontalOffset;
}
public void SetVerticalTilt(float verticalTilt)
{
this.verticalTilt = verticalTilt;
}
public void SetVerticalOffset(float verticalOffset)
{
this.verticalOffset = verticalOffset;
}
public void SetUseTargetNormal(bool useTargetNormal)
{
this.useTargetNormal = useTargetNormal;
}
public void SetCaptureCursor(bool captureCursor)
{
this.captureCursor = captureCursor;
}
public void SetHorizontalAxis(string horizontalAxis)
{
this.horizontalAxis = horizontalAxis;
}
public void SetHorizontalSensitivity(float horizontalSensitivity)
{
this.horizontalSensitivity = horizontalSensitivity;
}
public void SetInvertX(bool invertX)
{
this.invertX = invertX;
}
public void SetVerticalAxis(string verticalAxis)
{
this.verticalAxis = verticalAxis;
}
public void SetVerticalSensitivity(float verticalSensitivity)
{
this.verticalSensitivity = verticalSensitivity;
}
public void SetInvertY(bool invertY)
{
this.invertY = invertY;
}
public void SetZoomOutOnMotion(ZoomOutOnMotionEffect zoomOutOnMotion)
{
this.zoomOutOnMotion = zoomOutOnMotion;
}
public void SetMotionShake(MotionShakeEffect motionShake)
{
this.motionShake = motionShake;
}
#endregion
}