Shake.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Shake : MonoBehaviour {
  4. public delegate void OnShakeStopCallback(Shake shake);
  5. public Vector3 positionShake;
  6. public Vector3 angleShake;
  7. public float cycleTime = 0.2f;
  8. public int cycleCount = 6;
  9. public bool fixShake = false;
  10. public bool unscaleTime = false;
  11. public bool bothDir = true;
  12. public float fCycleCount = 0;
  13. public bool autoDisable = false;
  14. public OnShakeStopCallback onShakeStopCb = null;
  15. float currentTime;
  16. int curCycle;
  17. Vector3 curPositonShake;
  18. Vector3 curAngleShake;
  19. float curFovShake;
  20. Vector3 startPosition;
  21. Vector3 startAngles;
  22. Transform myTransform;
  23. void OnEnable()
  24. {
  25. Init(transform);
  26. }
  27. void OnDisable()
  28. {
  29. if (onShakeStopCb != null)
  30. onShakeStopCb(this);
  31. else
  32. {
  33. myTransform.localPosition = startPosition;
  34. myTransform.localEulerAngles = startAngles;
  35. }
  36. }
  37. // Update is called once per frame
  38. void Update () {
  39. float deltaTime = unscaleTime ? Time.unscaledDeltaTime : Time.deltaTime;
  40. UpdateFrame(deltaTime);
  41. }
  42. public void Init(Transform trans)
  43. {
  44. currentTime = 0f;
  45. curCycle = 0;
  46. curPositonShake = positionShake;
  47. curAngleShake = angleShake;
  48. myTransform = trans;
  49. startPosition = myTransform.localPosition;
  50. startAngles = myTransform.localEulerAngles;
  51. if (fCycleCount > 0)
  52. cycleCount = Mathf.RoundToInt(fCycleCount);
  53. }
  54. public void UpdateFrame(float deltaTime)
  55. {
  56. #if UNITY_EDITOR
  57. if (fCycleCount > 0)
  58. cycleCount = Mathf.RoundToInt(fCycleCount);
  59. #endif
  60. if (curCycle >= cycleCount)
  61. {
  62. if (autoDisable)
  63. enabled = false;
  64. return;
  65. }
  66. currentTime += deltaTime;
  67. while (currentTime >= cycleTime)
  68. {
  69. currentTime -= cycleTime;
  70. curCycle++;
  71. if (curCycle >= cycleCount)
  72. {
  73. myTransform.localPosition = startPosition;
  74. myTransform.localEulerAngles = startAngles;
  75. return;
  76. }
  77. if (!fixShake)
  78. {
  79. if (positionShake != Vector3.zero)
  80. curPositonShake = (cycleCount - curCycle) * positionShake / cycleCount;
  81. if (angleShake != Vector3.zero)
  82. curAngleShake = (cycleCount - curCycle) * angleShake / cycleCount;
  83. }
  84. }
  85. if (curCycle < cycleCount)
  86. {
  87. float offsetScale = Mathf.Sin((bothDir ? 2 : 1) * Mathf.PI * currentTime / cycleTime);
  88. if (positionShake != Vector3.zero)
  89. myTransform.localPosition = startPosition + curPositonShake * offsetScale;
  90. if (angleShake != Vector3.zero)
  91. myTransform.localEulerAngles = startAngles + curAngleShake * offsetScale;
  92. }
  93. }
  94. public void Restart()
  95. {
  96. if (enabled)
  97. {
  98. currentTime = 0f;
  99. curCycle = 0;
  100. curPositonShake = positionShake;
  101. curAngleShake = angleShake;
  102. myTransform.localPosition = startPosition;
  103. myTransform.localEulerAngles = startAngles;
  104. if (fCycleCount > 0)
  105. cycleCount = Mathf.RoundToInt(fCycleCount);
  106. }
  107. else
  108. enabled = true;
  109. }
  110. }