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
|
#if UNITY_EDITOR
using System;
using UnityEngine;
using XUtliPoolLib;
namespace XEditor
{
public class XCameraShake
{
private GameObject _gameObject = null;
private Camera _camera = null;
private float _timeEscaped = 0;
private float _timeInterval = 0;
private bool _shake = false;
private Vector3 x = Vector3.zero;
private Vector3 y = Vector3.zero;
private Vector3 z = Vector3.zero;
private float _time = 0;
private float _fovAmp = 0;
private float _amplitude_x = 0;
private float _amplitude_y = 0;
private float _amplitude_z = 0;
private float _frequency = 0;
private CameraMotionSpace _coordinate = CameraMotionSpace.World;
private bool _shakeX = true;
private bool _shakeY = true;
private bool _shakeZ = true;
private bool _random = false;
private float _fov = 0;
private int _rfactor = 1;
public XCameraShake(GameObject go, Camera camera)
{
_camera = camera;
_gameObject = go;
}
public bool OnShake(
float time,
float fovAmp,
float amplitude_x,
float amplitude_y,
float amplitude_z,
float frequency,
CameraMotionSpace coordinate,
bool shakeX,
bool shakeY,
bool shakeZ,
bool random)
{
_time = time;
_fovAmp = fovAmp;
_amplitude_x = amplitude_x;
_amplitude_y = amplitude_y;
_amplitude_z = amplitude_z;
_frequency = frequency;
_coordinate = coordinate;
_shakeX = shakeX;
_shakeY = shakeY;
_shakeZ = shakeZ;
_random = random;
_fov = _camera.fieldOfView;
if (null != _camera)
{
_timeEscaped = 0;
_timeInterval = 0;
_shake = true;
switch (_coordinate)
{
case CameraMotionSpace.Camera:
{
x = _camera.transform.right;
y = _camera.transform.up;
z = _camera.transform.forward;
} break;
case CameraMotionSpace.Self:
{
x = _gameObject.transform.right;
y = _gameObject.transform.up;
z = _gameObject.transform.forward;
} break;
case CameraMotionSpace.World:
{
x = Vector3.right;
y = Vector3.up;
z = Vector3.forward;
} break;
}
}
_rfactor = 1;
return true;
}
public void Update(float fDeltaT)
{
if (null != _camera && _shake)
{
_timeEscaped += fDeltaT;
_timeInterval += fDeltaT;
if (XCommon.singleton.IsGreater(_timeEscaped, _time))
{
StopShake();
}
else
{
if (XCommon.singleton.IsGreater(_timeInterval, 1 / _frequency))
{
_rfactor = -_rfactor;
_camera.transform.position += Shake();
float fov = UnityEngine.Random.Range(-_fovAmp, _fovAmp);
_camera.fieldOfView = _fov + (_random ? fov : _fovAmp * _rfactor);
_timeInterval = 0;
}
}
}
}
private void StopShake()
{
_timeEscaped = 0;
_shake = false;
_camera.fieldOfView = _fov;
}
private Vector3 Shake()
{
float offsetX = _random ? UnityEngine.Random.Range(-_amplitude_x, _amplitude_x) : _amplitude_x * _rfactor;
float offsetY = _random ? UnityEngine.Random.Range(-_amplitude_y, _amplitude_y) : _amplitude_y * _rfactor;
float offsetZ = _random ? UnityEngine.Random.Range(-_amplitude_z, _amplitude_z) : _amplitude_z * _rfactor;
Vector3 v = Vector3.zero;
if (_shakeX) v += (x * offsetX);
if (_shakeY) v += (y * offsetY);
if (_shakeZ) v += (z * offsetZ);
return v;
}
}
}
#endif
|