-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamController.cs
356 lines (321 loc) · 11.7 KB
/
CamController.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
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
public class CamController : MonoBehaviour
{
[Min(0f)]
[Tooltip("General movement speed")]
public float MovementSpeed = 7.5f;
[Min(0f)]
[Tooltip("Rotation speed")]
public float RotationSpeed = 100.0f;
[Min(0f)]
[Tooltip("Vertical movement speed")]
public float VerticalMovementSpeed = 2.5f;
[Min(0f)]
[Tooltip("Duration required for the camera to accelerate and decelerate if easing is enabled")]
public float EasingTime = 0.2f;
[Min(0f)]
[Tooltip("This value indicates how quickly the general movement speed can be changed per second via gamepad input")]
public float XZSpeedChangeRate = 5.0f;
[Min(0f)]
[Tooltip("This value indicates how quickly the vertical movement speed can be changed per second via gamepad input")]
public float YSpeedChangeRate = 2.0f;
[Min(0f)]
[Tooltip("This value indicates how quickly the rotation speed can be changed per second via gamepad input")]
public float RotationSpeedChangeRate = 20.0f;
[Min(0f)]
[Tooltip("This value indicates how quickly the easing time can be changed per second via gamepad input")]
public float EasingTimeChangeRate = 0.1f;
[Min(0f)]
[Tooltip("This value indicates how quickly the FOV (field of view) value can be changed per second via gamepad input")]
public float FOVChangeRate = 10.0f;
[Tooltip("Should the camera movement be restricted to the X and Z axes?")]
public bool RestrictYMovement = false;
[Tooltip("Should the camera accelerate/decelerate at the start/end of movement input or abruptly start/stop at a constant speed?")]
public bool EasingEnabled = true;
[Tooltip("Which layers should be accessible by the fly-to function?")]
public LayerMask TeleportLayerMask;
private GameObject _imgCrosshair;
private GameObject _panelBlackBar;
private TextMeshProUGUI _txtSpeed;
private TextMeshProUGUI _txtFOV;
private TextMeshProUGUI _txtRestrictY;
private TextMeshProUGUI _txtEasing;
private float _verticalMovement;
private float _xRotation = 0f;
private float _yRotation = 0f;
private Vector3 _currentVelocity;
private float _currentVerticalVelocity;
private Camera _cameraComponent;
private float _initialFOV;
private bool _isTeleporting = false;
private Vector3 _teleportDestination;
private float _teleportStartTime;
private int _camUiState = 2;
private float _lastDpadRightPressTime = 0f;
private const float _doublePressInterval = 0.5f;
private void Start()
{
_cameraComponent = Camera.main;
_initialFOV = _cameraComponent.fieldOfView;
_txtSpeed = GameObject.Find("TxtSpeed")?.GetComponent<TextMeshProUGUI>();
_txtFOV = GameObject.Find("TxtFOV")?.GetComponent<TextMeshProUGUI>();
_txtRestrictY = GameObject.Find("TxtRestrictY")?.GetComponent<TextMeshProUGUI>();
_txtEasing = GameObject.Find("TxtEasing")?.GetComponent<TextMeshProUGUI>();
_imgCrosshair = GameObject.Find("ImgCrosshair");
_panelBlackBar = GameObject.Find("BlackBar");
StartCoroutine(UpdateTextCoroutine());
}
void Update()
{
// Perform the following calls only if no fly-to is currently in progress
if (!_isTeleporting)
{
HandleMovement();
HandleRotation();
HandleVerticalMovement();
HandleFOVChange();
RestoreInitialFOV();
HandleTeleport();
}
else
{
TeleportToDestination();
}
HandleUIState();
HandleSpeedAdjustments();
ToggleEasing();
HandleDpadRightDoublePress();
}
/// <summary>
/// Coroutine - Update the texts every 0.1 seconds.
/// </summary>
IEnumerator UpdateTextCoroutine()
{
while (true)
{
if (_txtSpeed) _txtSpeed.text = $"Speed: {MovementSpeed.ToString("F2")} // {VerticalMovementSpeed.ToString("F2")} // {RotationSpeed.ToString("F2")}";
if (_txtFOV) _txtFOV.text = $"FOV: {_cameraComponent.fieldOfView.ToString("F2")} // {FOVChangeRate.ToString("F2")}";
if (_txtRestrictY) _txtRestrictY.text = $"Restrict Y: {(RestrictYMovement ? "On" : "Off")}";
if (_txtEasing) _txtEasing.text = $"Easing: {(EasingEnabled ? "On" : "Off")} // {EasingTime.ToString("F2")}";
yield return new WaitForSeconds(0.1f);
}
}
/// <summary>
/// Reset FOV to its initial value.
/// </summary>
void RestoreInitialFOV()
{
if (Gamepad.current.startButton.wasPressedThisFrame)
{
_cameraComponent.fieldOfView = _initialFOV;
}
}
/// <summary>
/// Fire a raycast to check if fly-to should be executed.
/// </summary>
void HandleTeleport()
{
if (Gamepad.current.leftStickButton.isPressed)
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, TeleportLayerMask))
{
_isTeleporting = true;
_teleportDestination = hit.point - 0.5f * transform.forward;
_teleportStartTime = Time.time;
_currentVelocity = Vector3.zero;
}
}
}
/// <summary>
/// Perform fly-to logic.
/// </summary>
void TeleportToDestination()
{
float duration = 0.4f;
float fraction = (Time.time - _teleportStartTime) / duration;
if (fraction < 1.0f)
{
transform.position = Vector3.Lerp(transform.position, _teleportDestination, fraction);
}
else
{
transform.position = _teleportDestination;
_isTeleporting = false;
}
}
/// <summary>
/// Adjust FOV (field of view) of the main camera.
/// </summary>
void HandleFOVChange()
{
if (Gamepad.current.buttonWest.isPressed)
{
_cameraComponent.fieldOfView += FOVChangeRate * Time.deltaTime;
}
else if (Gamepad.current.buttonNorth.isPressed)
{
_cameraComponent.fieldOfView -= FOVChangeRate * Time.deltaTime;
}
}
/// <summary>
/// Perform general camera movement.
/// </summary>
void HandleMovement()
{
float effectiveEasingTime = EasingEnabled ? EasingTime : 0f;
Vector2 moveInput = Gamepad.current.leftStick.ReadValue();
Vector3 targetVelocity;
float rightTriggerValue = Gamepad.current.rightTrigger.ReadValue();
float leftTriggerValue = Gamepad.current.leftTrigger.ReadValue();
float speedMultiplier = 1.0f;
if (rightTriggerValue > 0)
{
speedMultiplier = 2.5f;
}
else if (leftTriggerValue > 0)
{
speedMultiplier = 0.5f;
}
if (RestrictYMovement)
{
targetVelocity = (transform.right * moveInput.x + transform.forward * moveInput.y) * MovementSpeed * speedMultiplier;
targetVelocity.y = 0;
}
else
{
targetVelocity = transform.rotation * new Vector3(moveInput.x, 0, moveInput.y) * MovementSpeed * speedMultiplier;
}
_currentVelocity = Vector3.Lerp(_currentVelocity, targetVelocity, Time.deltaTime / effectiveEasingTime);
transform.Translate(_currentVelocity * Time.deltaTime, Space.World);
}
/// <summary>
/// Perform camera rotation.
/// </summary>
void HandleRotation()
{
Vector2 rotateInput = Gamepad.current.rightStick.ReadValue();
_xRotation -= rotateInput.y * RotationSpeed * Time.deltaTime;
_xRotation = Mathf.Clamp(_xRotation, -180f, 180f);
_yRotation += rotateInput.x * RotationSpeed * Time.deltaTime;
transform.localRotation = Quaternion.Euler(_xRotation, _yRotation, 0);
}
/// <summary>
/// Perform vertical camera movement.
/// </summary>
void HandleVerticalMovement()
{
float effectiveEasingTime = EasingEnabled ? EasingTime : 0f;
float rightTriggerValue = Gamepad.current.rightTrigger.ReadValue();
float leftTriggerValue = Gamepad.current.leftTrigger.ReadValue();
float speedMultiplier = 1.0f;
if (rightTriggerValue > 0)
{
speedMultiplier = 2.5f;
}
else if (leftTriggerValue > 0)
{
speedMultiplier = 0.5f;
}
if (Gamepad.current.buttonSouth.isPressed)
_verticalMovement = 1.0f;
else if (Gamepad.current.buttonEast.isPressed)
_verticalMovement = -1.0f;
else
_verticalMovement = 0;
float targetVerticalVelocity = _verticalMovement * VerticalMovementSpeed * speedMultiplier;
_currentVerticalVelocity = Mathf.Lerp(_currentVerticalVelocity, targetVerticalVelocity, Time.deltaTime / effectiveEasingTime);
transform.Translate(Vector3.up * _currentVerticalVelocity * Time.deltaTime, Space.World);
}
/// <summary>
/// Toggle between UI display states.
/// </summary>
void HandleUIState()
{
if (Gamepad.current.selectButton.wasPressedThisFrame)
{
switch (_camUiState)
{
case 0:
if (_panelBlackBar) _panelBlackBar.SetActive(true);
if (_imgCrosshair) _imgCrosshair.SetActive(false);
_camUiState = 1;
break;
case 1:
if (_panelBlackBar) _panelBlackBar.SetActive(true);
if (_imgCrosshair) _imgCrosshair.SetActive(true);
_camUiState = 2;
break;
case 2:
if (_panelBlackBar) _panelBlackBar.SetActive(false);
if (_imgCrosshair) _imgCrosshair.SetActive(false);
_camUiState = 0;
break;
}
}
}
/// <summary>
/// Handle all the available speed settings.
/// </summary>
void HandleSpeedAdjustments()
{
bool leftShoulder = Gamepad.current.leftShoulder.isPressed;
bool rightShoulder = Gamepad.current.rightShoulder.isPressed;
if (leftShoulder || rightShoulder)
{
float delta = (rightShoulder ? 1 : -1) * Time.deltaTime;
if (Gamepad.current.dpad.up.isPressed)
{
VerticalMovementSpeed += delta * YSpeedChangeRate;
}
else if (Gamepad.current.dpad.right.isPressed)
{
RotationSpeed += delta * RotationSpeedChangeRate;
}
else if (Gamepad.current.dpad.down.isPressed)
{
EasingTime += delta * EasingTimeChangeRate;
}
else if (Gamepad.current.dpad.left.isPressed)
{
FOVChangeRate += delta * 0.25f;
}
else
{
MovementSpeed += delta * XZSpeedChangeRate;
}
}
}
/// <summary>
/// Enable or disable easing.
/// </summary>
void ToggleEasing()
{
if (Gamepad.current.rightStickButton.wasPressedThisFrame)
{
EasingEnabled = !EasingEnabled;
}
}
/// <summary>
/// Reduce or allow movement on the Y-axis.
/// </summary>
void HandleDpadRightDoublePress()
{
if (Gamepad.current.dpad.right.wasPressedThisFrame)
{
if (Time.time - _lastDpadRightPressTime < _doublePressInterval)
{
RestrictYMovement = !RestrictYMovement;
_lastDpadRightPressTime = 0f;
}
else
{
_lastDpadRightPressTime = Time.time;
}
}
}
}