LightningWhipSpell.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Procedural Lightning for Unity
  3. // (c) 2015 Digital Ruby, LLC
  4. // Source code may be used for personal or commercial projects.
  5. // Source code may NOT be redistributed or sold.
  6. //
  7. using UnityEngine;
  8. using System.Collections;
  9. using System;
  10. namespace DigitalRuby.ThunderAndLightning
  11. {
  12. /// <summary>
  13. /// Lightning whip spell, think Balrog but with lightning instead of fire
  14. /// </summary>
  15. public class LightningWhipSpell : LightningSpellScript
  16. {
  17. /// <summary>Attach the whip to what object</summary>
  18. [Header("Whip")]
  19. [Tooltip("Attach the whip to what object")]
  20. public GameObject AttachTo;
  21. /// <summary>Rotate the whip with this object</summary>
  22. [Tooltip("Rotate the whip with this object")]
  23. public GameObject RotateWith;
  24. /// <summary>Whip handle</summary>
  25. [Tooltip("Whip handle")]
  26. public GameObject WhipHandle;
  27. /// <summary>Whip start</summary>
  28. [Tooltip("Whip start")]
  29. public GameObject WhipStart;
  30. /// <summary>Whip spring</summary>
  31. [Tooltip("Whip spring")]
  32. public GameObject WhipSpring;
  33. /// <summary>Whip crack audio source</summary>
  34. [Tooltip("Whip crack audio source")]
  35. public AudioSource WhipCrackAudioSource;
  36. /// <summary>
  37. /// Callback for when the whip strikes a point
  38. /// </summary>
  39. [HideInInspector]
  40. public Action<Vector3> CollisionCallback;
  41. private IEnumerator WhipForward()
  42. {
  43. const float distanceBack = 25.0f;
  44. const float springForwardTime = 0.10f;
  45. const float springBackwardTime = 0.25f;
  46. const float strikeWaitTime = 0.1f;
  47. const float recoilWaitTime = 0.1f;
  48. // remove the drag from all objects so they can move rapidly without decay
  49. for (int i = 0; i < WhipStart.transform.childCount; i++)
  50. {
  51. GameObject obj = WhipStart.transform.GetChild(i).gameObject;
  52. Rigidbody rb = obj.GetComponent<Rigidbody>();
  53. if (rb != null)
  54. {
  55. rb.drag = 0.0f;
  56. rb.velocity = Vector3.zero;
  57. rb.angularVelocity = Vector3.zero;
  58. }
  59. }
  60. // activate the spring
  61. WhipSpring.SetActive(true);
  62. Vector3 anchor = WhipStart.GetComponent<Rigidbody>().position;
  63. // calculate the forward position first
  64. RaycastHit hit;
  65. Vector3 whipPositionForwards, whipPositionBackwards;
  66. if (Physics.Raycast(anchor, Direction, out hit, MaxDistance, CollisionMask))
  67. {
  68. Vector3 dir = (hit.point - anchor).normalized;
  69. whipPositionForwards = anchor + (dir * MaxDistance);
  70. // put the spring behind the whip to yank it back in the opposite of the direction
  71. whipPositionBackwards = anchor - (dir * distanceBack);
  72. }
  73. else
  74. {
  75. whipPositionForwards = anchor + (Direction * MaxDistance);
  76. // put the spring behind the whip to yank it back in the opposite of the direction
  77. whipPositionBackwards = anchor - (Direction * distanceBack);
  78. }
  79. //whipPositionBackwards -= (WhipStart.transform.forward * distanceBack);
  80. //whipPositionBackwards += (WhipStart.transform.up * 5.0f);
  81. // set back position
  82. WhipSpring.GetComponent<Rigidbody>().position = whipPositionBackwards;
  83. // wait a bit
  84. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(springBackwardTime);
  85. // now put the spring in front of the whip to pull it forward
  86. WhipSpring.GetComponent<Rigidbody>().position = whipPositionForwards;
  87. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(springForwardTime);
  88. // play whip crack sound
  89. if (WhipCrackAudioSource != null)
  90. {
  91. WhipCrackAudioSource.Play();
  92. }
  93. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(strikeWaitTime);
  94. // show the strike paticle system
  95. if (CollisionParticleSystem != null)
  96. {
  97. CollisionParticleSystem.Play();
  98. }
  99. // create collision wherever the whip hit
  100. ApplyCollisionForce(SpellEnd.transform.position);
  101. // turn off the spring
  102. WhipSpring.SetActive(false);
  103. if (CollisionCallback != null)
  104. {
  105. CollisionCallback(SpellEnd.transform.position);
  106. }
  107. // wait a bit longer for the whip to recoil
  108. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(recoilWaitTime);
  109. // put the drag back on
  110. for (int i = 0; i < WhipStart.transform.childCount; i++)
  111. {
  112. GameObject obj = WhipStart.transform.GetChild(i).gameObject;
  113. Rigidbody rb = obj.GetComponent<Rigidbody>();
  114. if (rb != null)
  115. {
  116. rb.velocity = Vector3.zero;
  117. rb.angularVelocity = Vector3.zero;
  118. rb.drag = 0.5f;
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// Start
  124. /// </summary>
  125. protected override void Start()
  126. {
  127. base.Start();
  128. WhipSpring.SetActive(false);
  129. WhipHandle.SetActive(false);
  130. }
  131. /// <summary>
  132. /// Update
  133. /// </summary>
  134. protected override void Update()
  135. {
  136. base.Update();
  137. gameObject.transform.position = AttachTo.transform.position;
  138. gameObject.transform.rotation = RotateWith.transform.rotation;
  139. }
  140. /// <summary>
  141. /// Fires when spell is cast
  142. /// </summary>
  143. protected override void OnCastSpell()
  144. {
  145. StartCoroutine(WhipForward());
  146. }
  147. /// <summary>
  148. /// Fires when spell is stopped
  149. /// </summary>
  150. protected override void OnStopSpell()
  151. {
  152. }
  153. /// <summary>
  154. /// Fires when spell is activated
  155. /// </summary>
  156. protected override void OnActivated()
  157. {
  158. base.OnActivated();
  159. WhipHandle.SetActive(true);
  160. }
  161. /// <summary>
  162. /// Fires when spell is deactivated
  163. /// </summary>
  164. protected override void OnDeactivated()
  165. {
  166. base.OnDeactivated();
  167. WhipHandle.SetActive(false);
  168. }
  169. }
  170. }