Repository navigation
Expand file tree
/
Copy pathSimpleCameraController.cs
More file actions
205 lines (165 loc) · 7.51 KB
/
Copy pathSimpleCameraController.cs
File metadata and controls
205 lines (165 loc) · 7.51 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
using UnityEngine;
using UnityEngine.InputSystem;
using Unity.RenderStreaming;
namespace UnityTemplateProjects
{
public class SimpleCameraController : MonoBehaviour
{
class CameraState
{
public float yaw;
public float pitch;
public float roll;
public float x;
public float y;
public float z;
public void SetFromTransform(Transform t)
{
pitch = t.eulerAngles.x;
yaw = t.eulerAngles.y;
roll = t.eulerAngles.z;
x = t.position.x;
y = t.position.y;
z = t.position.z;
}
public void Translate(Vector3 translation)
{
Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;
x += rotatedTranslation.x;
y += rotatedTranslation.y;
z += rotatedTranslation.z;
}
public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
{
yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
x = Mathf.Lerp(x, target.x, positionLerpPct);
y = Mathf.Lerp(y, target.y, positionLerpPct);
z = Mathf.Lerp(z, target.z, positionLerpPct);
}
public void UpdateTransform(Transform t)
{
t.eulerAngles = new Vector3(pitch, yaw, roll);
t.position = new Vector3(x, y, z);
}
}
CameraState m_TargetCameraState = new CameraState();
CameraState m_InterpolatingCameraState = new CameraState();
[Header("Movement Settings")]
[Tooltip("Movement Sensitivity Factor."), Range(0.001f, 1f)]
[SerializeField] float m_movementSensitivityFactor = 0.1f;
[Tooltip("Exponential boost factor on translation, controllable by mouse wheel.")]
public float boost = 3.5f;
[Tooltip("Time it takes to interpolate camera position 99% of the way to the target."), Range(0.001f, 1f)]
public float positionLerpTime = 0.2f;
[Header("Rotation Settings")]
[Tooltip("X = Change in mouse position.\nY = Multiplicative factor for camera rotation.")]
public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));
[Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target."), Range(0.001f, 1f)]
public float rotationLerpTime = 0.01f;
[Tooltip("Whether or not to invert our Y axis for mouse input to rotation.")]
public bool invertY = false;
void OnEnable()
{
m_TargetCameraState.SetFromTransform(transform);
m_InterpolatingCameraState.SetFromTransform(transform);
}
//---------------------------------------------------------------------------------------------------------------------
Vector3 GetTranslationFromInput(Vector2 input) {
if (!invertY) {
input.y *= -1;
}
Vector3 dir = Vector3.right * input.x * m_movementSensitivityFactor;
dir += Vector3.back * input.y * m_movementSensitivityFactor;
return dir;
}
//---------------------------------------------------------------------------------------------------------------------
void UpdateTargetCameraStateFromInput(Vector2 input)
{
if (!invertY) {
input.y *= -1;
}
float mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(input.magnitude) * 0.1f;
m_TargetCameraState.yaw += input.x * mouseSensitivityFactor;
m_TargetCameraState.pitch += input.y * mouseSensitivityFactor;
}
//---------------------------------------------------------------------------------------------------------------------
Vector3 GetInputTranslationDirection()
{
Vector3 direction = new Vector3();
if (Keyboard.current.wKey.isPressed)
{
direction += Vector3.forward;
}
if (Keyboard.current.sKey.isPressed)
{
direction += Vector3.back;
}
if (Keyboard.current.aKey.isPressed)
{
direction += Vector3.left;
}
if (Keyboard.current.dKey.isPressed)
{
direction += Vector3.right;
}
if (Keyboard.current.qKey.isPressed)
{
direction += Vector3.down;
}
if (Keyboard.current.eKey.isPressed)
{
direction += Vector3.up;
}
//Translation
if (Touchscreen.current.activeTouches.Count == 2)
{
direction = GetTranslationFromInput(Touchscreen.current.activeTouches[0].delta.ReadValue());
} else if (IsMouseDragged(Mouse.current,true)) {
direction = GetTranslationFromInput(Mouse.current.delta.ReadValue());
} else if (IsMouseDragged(RemoteInput.RemoteMouse,true)) {
direction = GetTranslationFromInput(RemoteInput.RemoteMouse.delta.ReadValue());
}
return direction;
}
void FixedUpdate()
{
// Rotation
if (IsMouseDragged(Mouse.current,false)) {
UpdateTargetCameraStateFromInput(Mouse.current.delta.ReadValue());
} else if (IsMouseDragged(RemoteInput.RemoteMouse,false)) {
UpdateTargetCameraStateFromInput(RemoteInput.RemoteMouse.delta.ReadValue());
} else if (Touchscreen.current.activeTouches.Count == 1) {
UpdateTargetCameraStateFromInput(Touchscreen.current.activeTouches[0].delta.ReadValue());
}
// Translation
var translation = GetInputTranslationDirection() * Time.deltaTime;
// Speed up movement when shift key held
if (Keyboard.current.leftShiftKey.isPressed)
{
translation *= 10.0f;
}
translation *= Mathf.Pow(2.0f, boost);
m_TargetCameraState.Translate(translation);
// Framerate-independent interpolation
// Calculate the lerp amount, such that we get 99% of the way to our target in the specified time
var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);
m_InterpolatingCameraState.UpdateTransform(transform);
}
//---------------------------------------------------------------------------------------------------------------------
static bool IsMouseDragged(Mouse m, bool useLeftButton) {
if (null == m)
return false;
if (Screen.safeArea.Contains(m.position.ReadValue())) {
//check left/right click
if ((useLeftButton && m.leftButton.isPressed) || (!useLeftButton && m.rightButton.isPressed)) {
return true;
}
}
return false;
}
}
}