CameraShakeInstance.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEngine;
  2. using System.Collections;
  3. public enum CameraShakeState { FadingIn, FadingOut, Sustained, Inactive }
  4. public class CameraShakeInstance
  5. {
  6. /// <summary>
  7. /// The intensity of the shake. It is recommended that you use ScaleMagnitude to alter the magnitude of a shake.
  8. /// </summary>
  9. public float Magnitude;
  10. /// <summary>
  11. /// Roughness of the shake. It is recommended that you use ScaleRoughness to alter the roughness of a shake.
  12. /// </summary>
  13. public float Roughness;
  14. /// <summary>
  15. /// How much influence this shake has over the local position axes of the camera.
  16. /// </summary>
  17. public Vector3 PositionInfluence;
  18. /// <summary>
  19. /// How much influence this shake has over the local rotation axes of the camera.
  20. /// </summary>
  21. public Vector3 RotationInfluence;
  22. /// <summary>
  23. /// Should this shake be removed from the CameraShakeInstance list when not active?
  24. /// </summary>
  25. public bool DeleteOnInactive = true;
  26. float roughMod = 1, magnMod = 1;
  27. float fadeOutDuration, fadeInDuration;
  28. bool sustain;
  29. float currentFadeTime;
  30. float tick = 0;
  31. Vector3 amt;
  32. /// <summary>
  33. /// Will create a new instance that will shake once and fade over the given number of seconds.
  34. /// </summary>
  35. /// <param name="magnitude">The intensity of the shake.</param>
  36. /// <param name="fadeOutTime">How long, in seconds, to fade out the shake.</param>
  37. /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
  38. public CameraShakeInstance(float magnitude, float roughness, float fadeInTime, float fadeOutTime)
  39. {
  40. this.Magnitude = magnitude;
  41. fadeOutDuration = fadeOutTime;
  42. fadeInDuration = fadeInTime;
  43. this.Roughness = roughness;
  44. if (fadeInTime > 0)
  45. {
  46. sustain = true;
  47. currentFadeTime = 0;
  48. }
  49. else
  50. {
  51. sustain = false;
  52. currentFadeTime = 1;
  53. }
  54. tick = Random.Range(-100, 100);
  55. }
  56. /// <summary>
  57. /// Will create a new instance that will start a sustained shake.
  58. /// </summary>
  59. /// <param name="magnitude">The intensity of the shake.</param>
  60. /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
  61. public CameraShakeInstance(float magnitude, float roughness)
  62. {
  63. this.Magnitude = magnitude;
  64. this.Roughness = roughness;
  65. sustain = true;
  66. tick = Random.Range(-100, 100);
  67. }
  68. public Vector3 UpdateShake(float deltaTime)
  69. {
  70. amt.x = Mathf.PerlinNoise(tick, 0) - 0.5f;
  71. amt.y = Mathf.PerlinNoise(0, tick) - 0.5f;
  72. amt.z = Mathf.PerlinNoise(tick, tick) - 0.5f;
  73. if (fadeInDuration > 0 && sustain)
  74. {
  75. if (currentFadeTime < 1)
  76. currentFadeTime += deltaTime / fadeInDuration;
  77. else if(fadeOutDuration > 0)
  78. sustain = false;
  79. }
  80. if (!sustain)
  81. currentFadeTime -= deltaTime / fadeOutDuration;
  82. if(sustain)
  83. tick += deltaTime * Roughness * roughMod;
  84. else
  85. tick += deltaTime * Roughness * roughMod * currentFadeTime;
  86. return amt * Magnitude * magnMod * currentFadeTime;
  87. }
  88. /// <summary>
  89. /// Starts a fade out over the given number of seconds.
  90. /// </summary>
  91. /// <param name="fadeOutTime">The duration, in seconds, of the fade out.</param>
  92. public void StartFadeOut(float fadeOutTime)
  93. {
  94. if (fadeOutTime == 0)
  95. currentFadeTime = 0;
  96. fadeOutDuration = fadeOutTime;
  97. fadeInDuration = 0;
  98. sustain = false;
  99. }
  100. /// <summary>
  101. /// Starts a fade in over the given number of seconds.
  102. /// </summary>
  103. /// <param name="fadeInTime">The duration, in seconds, of the fade in.</param>
  104. public void StartFadeIn(float fadeInTime)
  105. {
  106. if (fadeInTime == 0)
  107. currentFadeTime = 1;
  108. fadeInDuration = fadeInTime;
  109. fadeOutDuration = 0;
  110. sustain = true;
  111. }
  112. /// <summary>
  113. /// Scales this shake's roughness while preserving the initial Roughness.
  114. /// </summary>
  115. public float ScaleRoughness
  116. {
  117. get { return roughMod;}
  118. set { roughMod = value; }
  119. }
  120. /// <summary>
  121. /// Scales this shake's magnitude while preserving the initial Magnitude.
  122. /// </summary>
  123. public float ScaleMagnitude
  124. {
  125. get { return magnMod; }
  126. set { magnMod = value; }
  127. }
  128. /// <summary>
  129. /// A normalized value (about 0 to about 1) that represents the current level of intensity.
  130. /// </summary>
  131. public float NormalizedFadeTime
  132. { get { return currentFadeTime; } }
  133. bool IsShaking
  134. { get { return currentFadeTime > 0 || sustain; } }
  135. bool IsFadingOut
  136. { get { return !sustain && currentFadeTime > 0; } }
  137. bool IsFadingIn
  138. { get { return currentFadeTime < 1 && sustain && fadeInDuration > 0; } }
  139. /// <summary>
  140. /// Gets the current state of the shake.
  141. /// </summary>
  142. public CameraShakeState CurrentState
  143. {
  144. get
  145. {
  146. if (IsFadingIn)
  147. return CameraShakeState.FadingIn;
  148. else if (IsFadingOut)
  149. return CameraShakeState.FadingOut;
  150. else if (IsShaking)
  151. return CameraShakeState.Sustained;
  152. else
  153. return CameraShakeState.Inactive;
  154. }
  155. }
  156. }