CameraShaker.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. [AddComponentMenu("EZ Camera Shake/Camera Shaker")]
  5. public class CameraShaker : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// The single instance of the CameraShake in the current scene. Do not use if you have multiple instances.
  9. /// </summary>
  10. public static CameraShaker Instance;
  11. static Dictionary<string, CameraShaker> instanceList = new Dictionary<string,CameraShaker>();
  12. /// <summary>
  13. /// The default position influcence of all shakes created by this shaker.
  14. /// </summary>
  15. public Vector3 DefaultPosInfluence = new Vector3(0.15f, 0.15f, 0.15f);
  16. /// <summary>
  17. /// The default rotation influcence of all shakes created by this shaker.
  18. /// </summary>
  19. public Vector3 DefaultRotInfluence = new Vector3(1, 1, 1);
  20. Vector3 posAddShake, rotAddShake;
  21. private Vector3 prePosAddShake, preRotAddShake;
  22. List<CameraShakeInstance> cameraShakeInstances = new List<CameraShakeInstance>();
  23. void Awake()
  24. {
  25. Instance = this;
  26. if(!instanceList.ContainsKey(gameObject.name))
  27. instanceList.Add(gameObject.name, this);
  28. }
  29. void Start()
  30. {
  31. prePosAddShake = Vector3.zero;
  32. preRotAddShake = Vector3.zero;
  33. }
  34. void Update()
  35. {
  36. posAddShake = Vector3.zero;
  37. rotAddShake = Vector3.zero;
  38. for (int i = 0; i < cameraShakeInstances.Count; i++)
  39. {
  40. if (i >= cameraShakeInstances.Count)
  41. break;
  42. CameraShakeInstance c = cameraShakeInstances[i];
  43. if (c.CurrentState == CameraShakeState.Inactive && c.DeleteOnInactive)
  44. {
  45. cameraShakeInstances.RemoveAt(i);
  46. i--;
  47. }
  48. else if (c.CurrentState != CameraShakeState.Inactive)
  49. {
  50. posAddShake += CameraUtilities.MultiplyVectors(c.UpdateShake(Time.deltaTime), c.PositionInfluence);
  51. rotAddShake += CameraUtilities.MultiplyVectors(c.UpdateShake(Time.deltaTime), c.RotationInfluence);
  52. }
  53. }
  54. transform.localPosition += posAddShake - prePosAddShake;
  55. transform.localEulerAngles += rotAddShake - preRotAddShake;
  56. prePosAddShake = posAddShake;
  57. preRotAddShake = rotAddShake;
  58. }
  59. /// <summary>
  60. /// Gets the CameraShaker with the given name, if it exists.
  61. /// </summary>
  62. /// <param name="name">The name of the camera shaker instance.</param>
  63. /// <returns></returns>
  64. public static CameraShaker GetInstance(string name)
  65. {
  66. CameraShaker c;
  67. if (instanceList.TryGetValue(name, out c))
  68. return c;
  69. Debug.LogError("CameraShake " + name + " not found!");
  70. return null;
  71. }
  72. /// <summary>
  73. /// Starts a shake using the given preset.
  74. /// </summary>
  75. /// <param name="shake">The preset to use.</param>
  76. /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
  77. public CameraShakeInstance Shake(CameraShakeInstance shake)
  78. {
  79. cameraShakeInstances.Add(shake);
  80. return shake;
  81. }
  82. /// <summary>
  83. /// Shake the camera once, fading in and out over a specified durations.
  84. /// </summary>
  85. /// <param name="magnitude">The intensity of the shake.</param>
  86. /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
  87. /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
  88. /// <param name="fadeOutTime">How long to fade out the shake, in seconds.</param>
  89. /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
  90. public CameraShakeInstance ShakeOnce(float magnitude, float roughness, float fadeInTime, float fadeOutTime)
  91. {
  92. CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness, fadeInTime, fadeOutTime);
  93. shake.PositionInfluence = DefaultPosInfluence;
  94. shake.RotationInfluence = DefaultRotInfluence;
  95. cameraShakeInstances.Add(shake);
  96. return shake;
  97. }
  98. /// <summary>
  99. /// Shake the camera once, fading in and out over a specified durations.
  100. /// </summary>
  101. /// <param name="magnitude">The intensity of the shake.</param>
  102. /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
  103. /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
  104. /// <param name="fadeOutTime">How long to fade out the shake, in seconds.</param>
  105. /// <param name="posInfluence">How much this shake influences position.</param>
  106. /// <param name="rotInfluence">How much this shake influences rotation.</param>
  107. /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
  108. public CameraShakeInstance ShakeOnce(float magnitude, float roughness, float fadeInTime, float fadeOutTime, Vector3 posInfluence, Vector3 rotInfluence)
  109. {
  110. CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness, fadeInTime, fadeOutTime);
  111. shake.PositionInfluence = posInfluence;
  112. shake.RotationInfluence = rotInfluence;
  113. cameraShakeInstances.Add(shake);
  114. return shake;
  115. }
  116. /// <summary>
  117. /// Start shaking the camera.
  118. /// </summary>
  119. /// <param name="magnitude">The intensity of the shake.</param>
  120. /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
  121. /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
  122. /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
  123. public CameraShakeInstance StartShake(float magnitude, float roughness, float fadeInTime)
  124. {
  125. CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness);
  126. shake.PositionInfluence = DefaultPosInfluence;
  127. shake.RotationInfluence = DefaultRotInfluence;
  128. shake.StartFadeIn(fadeInTime);
  129. cameraShakeInstances.Add(shake);
  130. return shake;
  131. }
  132. /// <summary>
  133. /// Start shaking the camera.
  134. /// </summary>
  135. /// <param name="magnitude">The intensity of the shake.</param>
  136. /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
  137. /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
  138. /// <param name="posInfluence">How much this shake influences position.</param>
  139. /// <param name="rotInfluence">How much this shake influences rotation.</param>
  140. /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
  141. public CameraShakeInstance StartShake(float magnitude, float roughness, float fadeInTime, Vector3 posInfluence, Vector3 rotInfluence)
  142. {
  143. CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness);
  144. shake.PositionInfluence = posInfluence;
  145. shake.RotationInfluence = rotInfluence;
  146. shake.StartFadeIn(fadeInTime);
  147. cameraShakeInstances.Add(shake);
  148. return shake;
  149. }
  150. /// <summary>
  151. /// Gets a copy of the list of current camera shake instances.
  152. /// </summary>
  153. public List<CameraShakeInstance> ShakeInstances
  154. { get { return new List<CameraShakeInstance>(cameraShakeInstances); } }
  155. }