LightningWhipScript.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. namespace DigitalRuby.ThunderAndLightning
  10. {
  11. /// <summary>
  12. /// Lightning whip script
  13. /// </summary>
  14. [RequireComponent(typeof(AudioSource))]
  15. public class LightningWhipScript : MonoBehaviour
  16. {
  17. /// <summary>
  18. /// Whip crack sound
  19. /// </summary>
  20. public AudioClip WhipCrack;
  21. /// <summary>
  22. /// Whip crack thunder sound
  23. /// </summary>
  24. public AudioClip WhipCrackThunder;
  25. private AudioSource audioSource;
  26. private GameObject whipStart;
  27. private GameObject whipEndStrike;
  28. private GameObject whipHandle;
  29. private GameObject whipSpring;
  30. private Vector2 prevDrag;
  31. private bool dragging;
  32. private bool canWhip = true;
  33. private IEnumerator WhipForward()
  34. {
  35. if (canWhip)
  36. {
  37. // first turn off whip, like a cooldown
  38. canWhip = false;
  39. // remove the drag from all objects so they can move rapidly without decay
  40. for (int i = 0; i < whipStart.transform.childCount; i++)
  41. {
  42. GameObject obj = whipStart.transform.GetChild(i).gameObject;
  43. Rigidbody2D rb = obj.GetComponent<Rigidbody2D>();
  44. if (rb != null)
  45. {
  46. rb.drag = 0.0f;
  47. }
  48. }
  49. // play the whip whoosh and crack sound
  50. audioSource.PlayOneShot(WhipCrack);
  51. // enable the spring and put it behind the whip to yank it back
  52. whipSpring.GetComponent<SpringJoint2D>().enabled = true;
  53. whipSpring.GetComponent<Rigidbody2D>().position = whipHandle.GetComponent<Rigidbody2D>().position + new Vector2(-15.0f, 5.0f);
  54. // wait a bit
  55. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(0.2f);
  56. // now put the spring in front of the whip to pull it forward
  57. whipSpring.GetComponent<Rigidbody2D>().position = whipHandle.GetComponent<Rigidbody2D>().position + new Vector2(15.0f, 2.5f);
  58. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(0.15f);
  59. audioSource.PlayOneShot(WhipCrackThunder, 0.5f);
  60. // wait a bit
  61. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(0.15f);
  62. // show the strike paticle system
  63. whipEndStrike.GetComponent<ParticleSystem>().Play();
  64. // turn off the spring
  65. whipSpring.GetComponent<SpringJoint2D>().enabled = false;
  66. // wait a bit longer for the whip to recoil
  67. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(0.65f);
  68. // put the drag back on
  69. for (int i = 0; i < whipStart.transform.childCount; i++)
  70. {
  71. GameObject obj = whipStart.transform.GetChild(i).gameObject;
  72. Rigidbody2D rb = obj.GetComponent<Rigidbody2D>();
  73. if (rb != null)
  74. {
  75. rb.velocity = Vector2.zero;
  76. rb.drag = 0.5f;
  77. }
  78. }
  79. // now they can whip again
  80. canWhip = true;
  81. }
  82. }
  83. private void Start()
  84. {
  85. whipStart = GameObject.Find("WhipStart");
  86. whipEndStrike = GameObject.Find("WhipEndStrike");
  87. whipHandle = GameObject.Find("WhipHandle");
  88. whipSpring = GameObject.Find("WhipSpring");
  89. audioSource = GetComponent<AudioSource>();
  90. }
  91. private void Update()
  92. {
  93. if (!dragging && Input.GetMouseButtonDown(0))
  94. {
  95. Vector2 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  96. Collider2D hit = Physics2D.OverlapPoint(worldPos);
  97. if (hit != null && hit.gameObject == whipHandle)
  98. {
  99. dragging = true;
  100. prevDrag = worldPos;
  101. }
  102. }
  103. else if (dragging && Input.GetMouseButton(0))
  104. {
  105. Vector2 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  106. Vector2 offset = worldPos - prevDrag;
  107. Rigidbody2D rb = whipHandle.GetComponent<Rigidbody2D>();
  108. rb.MovePosition(rb.position + offset);
  109. prevDrag = worldPos;
  110. }
  111. else
  112. {
  113. dragging = false;
  114. }
  115. if (Input.GetKeyDown(KeyCode.Space))
  116. {
  117. StartCoroutine(WhipForward());
  118. }
  119. }
  120. }
  121. }