CameraShake.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace MirzaBeig
  5. {
  6. namespace ParticleSystems
  7. {
  8. namespace Demos
  9. {
  10. public enum CameraShakeTarget
  11. {
  12. Position,
  13. Rotation
  14. }
  15. public enum CameraShakeAmplitudeCurve
  16. {
  17. Constant,
  18. // Fade in fully at 25%, 50%, and 75% of lifetime.
  19. FadeInOut25,
  20. FadeInOut50,
  21. FadeInOut75,
  22. }
  23. public enum CameraShakeAmplitudeOverDistanceCurve
  24. {
  25. Constant,
  26. LinearFadeIn,
  27. LinearFadeOut
  28. }
  29. public class CameraShake : MonoBehaviour
  30. {
  31. [System.Serializable]
  32. public class Shake
  33. {
  34. public float amplitude = 1.0f;
  35. public float frequency = 1.0f;
  36. public float duration;
  37. [HideInInspector]
  38. public CameraShakeTarget target;
  39. float timeRemaining;
  40. Vector2 perlinNoiseX;
  41. Vector2 perlinNoiseY;
  42. Vector2 perlinNoiseZ;
  43. [HideInInspector]
  44. public Vector3 noise;
  45. public AnimationCurve amplitudeOverLifetimeCurve = new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 0.0f));
  46. public void Init()
  47. {
  48. timeRemaining = duration;
  49. ApplyRandomSeed();
  50. }
  51. void Init(float amplitude, float frequency, float duration, CameraShakeTarget target)
  52. {
  53. this.amplitude = amplitude;
  54. this.frequency = frequency;
  55. this.duration = duration;
  56. timeRemaining = duration;
  57. this.target = target;
  58. ApplyRandomSeed();
  59. }
  60. public void ApplyRandomSeed()
  61. {
  62. float randomRange = 32.0f;
  63. perlinNoiseX.x = Random.Range(-randomRange, randomRange);
  64. perlinNoiseX.y = Random.Range(-randomRange, randomRange);
  65. perlinNoiseY.x = Random.Range(-randomRange, randomRange);
  66. perlinNoiseY.y = Random.Range(-randomRange, randomRange);
  67. perlinNoiseZ.x = Random.Range(-randomRange, randomRange);
  68. perlinNoiseZ.y = Random.Range(-randomRange, randomRange);
  69. }
  70. public Shake(float amplitude, float frequency, float duration, CameraShakeTarget target, AnimationCurve amplitudeOverLifetimeCurve)
  71. {
  72. Init(amplitude, frequency, duration, target);
  73. this.amplitudeOverLifetimeCurve = amplitudeOverLifetimeCurve;
  74. }
  75. public Shake(float amplitude, float frequency, float duration, CameraShakeTarget target, CameraShakeAmplitudeCurve amplitudeOverLifetimeCurve)
  76. {
  77. Init(amplitude, frequency, duration, target);
  78. switch (amplitudeOverLifetimeCurve)
  79. {
  80. case CameraShakeAmplitudeCurve.Constant:
  81. {
  82. this.amplitudeOverLifetimeCurve = AnimationCurve.Linear(0.0f, 1.0f, 1.0f, 1.0f);
  83. break;
  84. }
  85. case CameraShakeAmplitudeCurve.FadeInOut25:
  86. {
  87. this.amplitudeOverLifetimeCurve = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(0.25f, 1.0f), new Keyframe(1.0f, 0.0f));
  88. break;
  89. }
  90. case CameraShakeAmplitudeCurve.FadeInOut50:
  91. {
  92. this.amplitudeOverLifetimeCurve = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(0.50f, 1.0f), new Keyframe(1.0f, 0.0f));
  93. break;
  94. }
  95. case CameraShakeAmplitudeCurve.FadeInOut75:
  96. {
  97. this.amplitudeOverLifetimeCurve = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(0.75f, 1.0f), new Keyframe(1.0f, 0.0f));
  98. break;
  99. }
  100. default:
  101. {
  102. throw new System.Exception("Unknown enum.");
  103. }
  104. }
  105. }
  106. public bool IsAlive()
  107. {
  108. return timeRemaining > 0.0f;
  109. }
  110. public void Update()
  111. {
  112. if (timeRemaining < 0.0f)
  113. {
  114. return;
  115. }
  116. Vector2 frequencyVector = Time.deltaTime * new Vector2(frequency, frequency);
  117. perlinNoiseX += frequencyVector;
  118. perlinNoiseY += frequencyVector;
  119. perlinNoiseZ += frequencyVector;
  120. noise.x = Mathf.PerlinNoise(perlinNoiseX.x, perlinNoiseX.y) - 0.5f;
  121. noise.y = Mathf.PerlinNoise(perlinNoiseY.x, perlinNoiseY.y) - 0.5f;
  122. noise.z = Mathf.PerlinNoise(perlinNoiseZ.x, perlinNoiseZ.y) - 0.5f;
  123. float amplitudeOverLifetime = amplitudeOverLifetimeCurve.Evaluate(1.0f - (timeRemaining / duration));
  124. noise *= amplitude * amplitudeOverLifetime;
  125. timeRemaining -= Time.deltaTime;
  126. }
  127. }
  128. // ...
  129. public float smoothDampTime = 0.025f;
  130. Vector3 smoothDampPositionVelocity;
  131. float smoothDampRotationVelocityX;
  132. float smoothDampRotationVelocityY;
  133. float smoothDampRotationVelocityZ;
  134. List<Shake> shakes = new List<Shake>();
  135. // ...
  136. void Start()
  137. {
  138. }
  139. // ...
  140. public void Add(float amplitude, float frequency, float duration, CameraShakeTarget target, AnimationCurve amplitudeOverLifetimeCurve)
  141. {
  142. shakes.Add(new Shake(amplitude, frequency, duration, target, amplitudeOverLifetimeCurve));
  143. }
  144. public void Add(float amplitude, float frequency, float duration, CameraShakeTarget target, CameraShakeAmplitudeCurve amplitudeOverLifetimeCurve)
  145. {
  146. shakes.Add(new Shake(amplitude, frequency, duration, target, amplitudeOverLifetimeCurve));
  147. }
  148. // ...
  149. void Update()
  150. {
  151. if (Input.GetKeyDown(KeyCode.F))
  152. {
  153. Add(0.25f, 1.0f, 2.0f, CameraShakeTarget.Position, CameraShakeAmplitudeCurve.FadeInOut25);
  154. }
  155. if (Input.GetKeyDown(KeyCode.G))
  156. {
  157. Add(15.0f, 1.0f, 2.0f, CameraShakeTarget.Rotation, CameraShakeAmplitudeCurve.FadeInOut25);
  158. }
  159. if (Input.GetKey(KeyCode.H))
  160. {
  161. }
  162. Vector3 positionOffset = Vector3.zero;
  163. Vector3 rotationOffset = Vector3.zero;
  164. for (int i = 0; i < shakes.Count; i++)
  165. {
  166. shakes[i].Update();
  167. if (shakes[i].target == CameraShakeTarget.Position)
  168. {
  169. positionOffset += shakes[i].noise;
  170. }
  171. else
  172. {
  173. rotationOffset += shakes[i].noise;
  174. }
  175. }
  176. shakes.RemoveAll(x => !x.IsAlive());
  177. transform.localPosition = Vector3.SmoothDamp(transform.localPosition, positionOffset, ref smoothDampPositionVelocity, smoothDampTime);
  178. Vector3 eulerAngles = transform.localEulerAngles;
  179. eulerAngles.x = Mathf.SmoothDampAngle(eulerAngles.x, rotationOffset.x, ref smoothDampRotationVelocityX, smoothDampTime);
  180. eulerAngles.y = Mathf.SmoothDampAngle(eulerAngles.y, rotationOffset.y, ref smoothDampRotationVelocityY, smoothDampTime);
  181. eulerAngles.z = Mathf.SmoothDampAngle(eulerAngles.z, rotationOffset.z, ref smoothDampRotationVelocityZ, smoothDampTime);
  182. transform.localEulerAngles = eulerAngles;
  183. }
  184. }
  185. }
  186. }
  187. }