LightningBeamSpellScript.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 bolt beam spell script, like a phasor or laser
  13. /// </summary>
  14. public class LightningBeamSpellScript : LightningSpellScript
  15. {
  16. /// <summary>The lightning path script creating the beam of lightning</summary>
  17. [Header("Beam")]
  18. [Tooltip("The lightning path script creating the beam of lightning")]
  19. public LightningBoltPathScriptBase LightningPathScript;
  20. /// <summary>Give the end point some randomization</summary>
  21. [Tooltip("Give the end point some randomization")]
  22. public float EndPointRandomization = 1.5f;
  23. /// <summary>
  24. /// Callback for collision events
  25. /// </summary>
  26. [HideInInspector]
  27. public System.Action<RaycastHit> CollisionCallback;
  28. private void CheckCollision()
  29. {
  30. RaycastHit hit;
  31. // send out a ray to see what gets hit
  32. if (Physics.Raycast(SpellStart.transform.position, Direction, out hit, MaxDistance, CollisionMask))
  33. {
  34. // we hit something, set the end object position
  35. SpellEnd.transform.position = hit.point;
  36. // additional randomization of end point
  37. SpellEnd.transform.position += (UnityEngine.Random.insideUnitSphere * EndPointRandomization);
  38. // play collision sound
  39. PlayCollisionSound(SpellEnd.transform.position);
  40. // play the collision particle system
  41. if (CollisionParticleSystem != null)
  42. {
  43. CollisionParticleSystem.transform.position = hit.point;
  44. CollisionParticleSystem.Play();
  45. }
  46. ApplyCollisionForce(hit.point);
  47. // notify listeners of collisions
  48. if (CollisionCallback != null)
  49. {
  50. CollisionCallback(hit);
  51. }
  52. }
  53. else
  54. {
  55. // stop collision particle system
  56. if (CollisionParticleSystem != null)
  57. {
  58. CollisionParticleSystem.Stop();
  59. }
  60. // extend beam to max length
  61. SpellEnd.transform.position = SpellStart.transform.position + (Direction * MaxDistance);
  62. // randomize end point a bit
  63. SpellEnd.transform.position += (UnityEngine.Random.insideUnitSphere * EndPointRandomization);
  64. }
  65. }
  66. /// <summary>
  67. /// Start
  68. /// </summary>
  69. protected override void Start()
  70. {
  71. base.Start();
  72. LightningPathScript.ManualMode = true;
  73. }
  74. /// <summary>
  75. /// Update
  76. /// </summary>
  77. protected override void LateUpdate()
  78. {
  79. base.LateUpdate();
  80. if (!Casting)
  81. {
  82. return;
  83. }
  84. CheckCollision();
  85. }
  86. /// <summary>
  87. /// OnCastSpell
  88. /// </summary>
  89. protected override void OnCastSpell()
  90. {
  91. LightningPathScript.ManualMode = false;
  92. }
  93. /// <summary>
  94. /// OnStopSpell
  95. /// </summary>
  96. protected override void OnStopSpell()
  97. {
  98. LightningPathScript.ManualMode = true;
  99. }
  100. }
  101. }