-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCamera.cs
96 lines (79 loc) · 2.14 KB
/
CCamera.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
using System;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CCamera : MonoBehaviour {
[Serializable]
public abstract class Module : MonoBehaviour
{
protected Transform cameraTarget;
public bool smoothCamera;
public float smoothMoveSpeed;
public float smoothRotationSpeed;
public void SetCameraTarget(Transform newTarget)
{
cameraTarget = newTarget;
}
public virtual void UpdateCamera(out Vector3 cameraPos, out Quaternion cameraRot)
{
if (!cameraTarget)
{
cameraPos = Vector3.zero;
cameraRot = Quaternion.identity;
return;
}
cameraPos = cameraTarget.position;
cameraRot = cameraTarget.rotation;
}
public virtual void Initialize(Transform cameraTransform)
{
}
}
public Transform cameraTarget;
[SerializeField]
public Module[] cameras;
private Module activeCamera;
private int activeCameraIndex;
private void Awake()
{
if (cameras == null
|| cameras[0] == null)
{
Debug.LogWarning("No default camera module set up. Destroying " + gameObject + " because of this.");
Destroy(gameObject);
return;
}
activeCameraIndex = 0;
SwitchToCamera(activeCameraIndex);
}
private void SwitchToCamera(int index)
{
cameras[index].SetCameraTarget(cameraTarget);
cameras[index].Initialize(transform);
activeCamera = cameras[index];
}
private void LateUpdate()
{
if (!activeCamera) return;
var cameraPos = transform.position;
var cameraRot = transform.rotation;
activeCamera.UpdateCamera(out cameraPos, out cameraRot);
if (!activeCamera.smoothCamera)
{
transform.position = cameraPos;
transform.rotation = cameraRot;
}
else
{
transform.position = Vector3.Lerp(transform.position, cameraPos, Time.deltaTime * activeCamera.smoothMoveSpeed);
transform.rotation = Quaternion.Lerp(transform.rotation, cameraRot, Time.deltaTime * activeCamera.smoothRotationSpeed);
}
}
public void SwitchCamera()
{
if (activeCameraIndex < cameras.Length)
activeCameraIndex++;
else
activeCameraIndex = 0;
SwitchToCamera(activeCameraIndex);
}
}