-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraUtils.cs
354 lines (322 loc) · 6.03 KB
/
CameraUtils.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
using UnityEngine;
using System.Collections;
public class CameraUtils : MonoBehaviour {
private Transform _POI;
private Camera _camera;
//Parameters
private float _followSpeed;
private Vector3 _offset;
//States
private bool _followX;
private bool _followY;
private bool _followZ;
private bool _fpv;
//Bounds
private bool _boundX;
private float _maxX;
private float _minX;
private bool _boundY;
private float _maxY;
private float _minY;
private bool _boundZ;
private float _maxZ;
private float _minZ;
//FPS mode
private float _sensitivityX = 15;
private float _sensitivityY = 15;
private float _angleX = 360;
private float _angleY = 60;
float rotationY = 0F;
//Get and sets
public float followSpeed {
get {
return(_followSpeed);
}
set {
_followSpeed = value;
}
}
public Camera camera {
get {
return _camera;
}
set {
_camera = value;
}
}
public Transform POI {
get {
return _POI;
}
set {
_POI = value;
}
}
public bool followX {
get {
return _followX;
}
set {
_followX = value;
}
}
public bool followY {
get {
return _followY;
}
set {
_followY = value;
}
}
public bool followZ {
get {
return _followZ;
}
set {
_followZ = value;
}
}
public bool fpv {
get {
return _fpv;
}
set {
_fpv = value;
}
}
public Vector3 offset {
get {
return _offset;
}
set {
_offset = value;
}
}
public bool boundX {
get {
return _boundX;
}
set {
_boundX = value;
}
}
public bool boundY {
get {
return _boundY;
}
set {
_boundY = value;
}
}
public bool boundZ {
get {
return _boundZ;
}
set {
_boundZ = value;
}
}
public float minX {
get {
return _minX;
}
set {
_minX = value;
_boundX = true;
}
}
public float minY {
get {
return _minY;
}
set {
_minY = value;
_boundY = true;
}
}
public float minZ {
get {
return _minZ;
}
set {
_minZ = value;
_boundZ = true;
}
}
public float maxX {
get {
return _maxX;
}
set {
_maxX = value;
_boundX = true;
}
}
public float maxY {
get {
return _maxY;
}
set {
_maxY = value;
_boundY = true;
}
}
public float maxZ {
get {
return _maxZ;
}
set {
_maxZ = value;
_boundZ = true;
}
}
public float sensitivityX {
get {
return _sensitivityX;
}
set {
_sensitivityX = value;
}
}
public float sensitivityY {
get {
return _sensitivityY;
}
set {
_sensitivityY = value;
}
}
public float angleX {
get {
return _angleX;
}
set {
_angleX = value;
}
}
public float angleY {
get {
return _angleY;
}
set {
_angleY = value;
}
}
//Defaults
void Awake() {
_followX = true;
_followY = true;
_followZ = true;
_fpv = false;
_followSpeed = 10;
_camera = Camera.main;
_offset = new Vector3(2,1,1);
_boundX = false;
_boundY = false;
_boundZ = false;
_minX = 0;
_minY = 0;
_minZ = 0;
_maxX = 0;
_maxY = 0;
_maxZ = 0;
_sensitivityX = 15;
_sensitivityY = 15;
_angleX = 360;
_angleY = 60;
}
// Update is called once per frame
void Update () {
if(!_fpv)
{
Vector3 target = _camera.transform.position;
Debug.Log ("Follows are " + followX + followY + followZ);
if(_followX) {
target.x = _POI.position.x;
if(_boundX && target.x < _minX)
target.x = _minX;
else if(_boundX && target.x > _maxX)
target.x = _maxX;
target.x += _offset.x;
}
if(_followY) {
target.y = _POI.position.y;
if(_boundY && target.y < _minY)
target.y = _minY;
else if(_boundY && target.y > _maxY)
target.y = _maxY;
target.z += _offset.z;
}
if(_followZ) {
target.z = _POI.position.z;
if(_boundZ && target.z < _minZ)
target.z = _minZ;
else if(_boundZ && target.z > _maxZ)
target.z = _maxZ;
target.z += _offset.z;
}
_camera.transform.position = Vector3.Lerp(_camera.transform.position, target, _followSpeed * Time.deltaTime);
//Vector3 pos = ((1 - _followSpeed/10.0f) * _camera.transform.position + _followSpeed/10.0f * target);
//_camera.transform.position = pos;
}
else fpsUpdate();
}
void fpsUpdate() {
float rotationX = _camera.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, -angleY, angleY);
_camera.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
//Methods
/* Add a camera shake for a definite period of time */
// Code based on http://unitytipsandtricks.blogspot.com/2013/05/camera-shake.html
public void shake(float duration, float magnitude) {
StartCoroutine(startShake (duration, magnitude));
}
private IEnumerator startShake(float duration, float magnitude) {
float time = 0.0f;
Vector3 initial = _camera.transform.position;
while (time < duration) {
time += Time.deltaTime;
float percentComplete = time / duration;
Debug.Log("done with " + percentComplete);
float damper = 1.0f - Mathf.Clamp(4.0f * percentComplete - 3.0f, 0.0f, 1.0f);
// map value to [-1, 1]
float x = Random.value * 2.0f - 1.0f;
float y = Random.value * 2.0f - 1.0f;
x *= magnitude * damper;
y *= magnitude * damper;
_camera.transform.position = new Vector3(x, y, initial.z);
yield return null;
}
_camera.transform.position = initial;
}
/* Smooth zoom in or out */
public void zoom(float speed, bool zoomIn) {
Vector3 dir = (_camera.transform.position - _POI.transform.position).normalized;
if(zoomIn) {
if(!_camera.isOrthoGraphic) {
_camera.transform.position -= dir * speed;
_offset -= dir;
}
else {
_camera.orthographicSize -=0.1f * speed;
_offset.x -= 0.05f * speed * _offset.x;
_offset.y -= 0.05f * speed * _offset.y;
_offset.z -= 0.05f * speed * _offset.z;
}
}
else {
if(!_camera.isOrthoGraphic) {
_camera.transform.position += dir * speed;
_offset -= dir;
}
else {
_camera.orthographicSize += 0.1f * speed;
_offset.x += 0.05f * speed;
_offset.y += 0.05f * speed;
_offset.z += 0.05f * speed;
}
}
}
}