LightningBoltShapeConeScript.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.Collections.Generic;
  10. namespace DigitalRuby.ThunderAndLightning
  11. {
  12. /// <summary>
  13. /// Lightning bolt cone shape script, draws lightning in a cone coming from inner radius spreading to outter radius at length
  14. /// </summary>
  15. public class LightningBoltShapeConeScript : LightningBoltPrefabScriptBase
  16. {
  17. /// <summary>Radius at base of cone where lightning can emit from</summary>
  18. [Header("Lightning Cone Properties")]
  19. [Tooltip("Radius at base of cone where lightning can emit from")]
  20. public float InnerRadius = 0.1f;
  21. /// <summary>Radius at outer part of the cone where lightning emits to</summary>
  22. [Tooltip("Radius at outer part of the cone where lightning emits to")]
  23. public float OuterRadius = 4.0f;
  24. /// <summary>The length of the cone from the center of the inner and outer circle</summary>
  25. [Tooltip("The length of the cone from the center of the inner and outer circle")]
  26. public float Length = 4.0f;
  27. #if UNITY_EDITOR
  28. /// <summary>
  29. /// OnDrawGizmos
  30. /// </summary>
  31. protected override void OnDrawGizmos()
  32. {
  33. base.OnDrawGizmos();
  34. UnityEditor.Handles.DrawWireDisc(transform.position, transform.forward, InnerRadius);
  35. UnityEditor.Handles.DrawWireDisc(transform.position + (transform.forward * Length), transform.forward, OuterRadius);
  36. UnityEditor.Handles.DrawLine(transform.position + (transform.rotation * Vector3.right * InnerRadius),
  37. transform.position + (transform.rotation * Vector3.right * OuterRadius) + (transform.forward * Length));
  38. UnityEditor.Handles.DrawLine(transform.position + (transform.rotation * -Vector3.right * InnerRadius),
  39. transform.position + (transform.rotation * -Vector3.right * OuterRadius) + (transform.forward * Length));
  40. UnityEditor.Handles.DrawLine(transform.position + (transform.rotation * Vector3.up * InnerRadius),
  41. transform.position + (transform.rotation * Vector3.up * OuterRadius) + (transform.forward * Length));
  42. UnityEditor.Handles.DrawLine(transform.position + (transform.rotation * -Vector3.up * InnerRadius),
  43. transform.position + (transform.rotation * -Vector3.up * OuterRadius) + (transform.forward * Length));
  44. }
  45. #endif
  46. /// <summary>
  47. /// Create a lightning bolt
  48. /// </summary>
  49. /// <param name="parameters">Parameters</param>
  50. public override void CreateLightningBolt(LightningBoltParameters parameters)
  51. {
  52. Vector2 circle1 = UnityEngine.Random.insideUnitCircle * InnerRadius;
  53. Vector3 start = transform.rotation * new Vector3(circle1.x, circle1.y, 0.0f);
  54. Vector2 circle2 = UnityEngine.Random.insideUnitCircle * OuterRadius;
  55. Vector3 end = (transform.rotation * new Vector3(circle2.x, circle2.y, 0.0f)) + (transform.forward * Length);
  56. parameters.Start = start;
  57. parameters.End = end;
  58. base.CreateLightningBolt(parameters);
  59. }
  60. }
  61. }