LightningLightsabreScript.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace DigitalRuby.ThunderAndLightning
  4. {
  5. /// <summary>
  6. /// Lightning bolt script that acts like a laser sword
  7. /// </summary>
  8. public class LightningLightsabreScript : LightningBoltPrefabScript
  9. {
  10. /// <summary>Height of the blade</summary>
  11. [Header("Lightsabre Properties")]
  12. [Tooltip("Height of the blade")]
  13. public float BladeHeight = 19.0f;
  14. /// <summary>How long it takes to turn the lightsabre on and off</summary>
  15. [Tooltip("How long it takes to turn the lightsabre on and off")]
  16. public float ActivationTime = 0.5f;
  17. /// <summary>Sound to play when the lightsabre turns on</summary>
  18. [Tooltip("Sound to play when the lightsabre turns on")]
  19. public AudioSource StartSound;
  20. /// <summary>Sound to play when the lightsabre turns off</summary>
  21. [Tooltip("Sound to play when the lightsabre turns off")]
  22. public AudioSource StopSound;
  23. /// <summary>Sound to play when the lightsabre stays on</summary>
  24. [Tooltip("Sound to play when the lightsabre stays on")]
  25. public AudioSource ConstantSound;
  26. private int state; // 0 = off, 1 = on, 2 = turning off, 3 = turning on
  27. private Vector3 bladeStart;
  28. private Vector3 bladeDir;
  29. private float bladeTime;
  30. private float bladeIntensity;
  31. /// <summary>
  32. /// Start
  33. /// </summary>
  34. protected override void Start()
  35. {
  36. base.Start();
  37. }
  38. /// <summary>
  39. /// Update
  40. /// </summary>
  41. protected override void Update()
  42. {
  43. if (state == 2 || state == 3)
  44. {
  45. bladeTime += LightningBoltScript.DeltaTime;
  46. float percent = Mathf.Lerp(0.01f, 1.0f, bladeTime / ActivationTime);
  47. Vector3 end = bladeStart + (bladeDir * percent * BladeHeight);
  48. Destination.transform.position = end;
  49. GlowIntensity = bladeIntensity * (state == 3 ? percent : (1.0f - percent));
  50. if (bladeTime >= ActivationTime)
  51. {
  52. GlowIntensity = bladeIntensity;
  53. bladeTime = 0.0f;
  54. if (state == 2)
  55. {
  56. ManualMode = true;
  57. state = 0;
  58. }
  59. else
  60. {
  61. state = 1;
  62. }
  63. }
  64. }
  65. base.Update();
  66. }
  67. /// <summary>
  68. /// True to turn on the lightsabre, false to turn it off
  69. /// </summary>
  70. /// <param name="value">Whether the lightsabre is on or off</param>
  71. /// <returns>True if success, false if invalid operation (i.e. lightsabre is already on or off)</returns>
  72. public bool TurnOn(bool value)
  73. {
  74. if (state == 2 || state == 3 || (state == 1 && value) || (state == 0 && !value))
  75. {
  76. return false;
  77. }
  78. bladeStart = Destination.transform.position;
  79. ManualMode = false;
  80. bladeIntensity = GlowIntensity;
  81. if (value)
  82. {
  83. bladeDir = (Camera.orthographic ? transform.up : transform.forward);
  84. state = 3;
  85. StartSound.Play();
  86. StopSound.Stop();
  87. ConstantSound.Play();
  88. }
  89. else
  90. {
  91. bladeDir = -(Camera.orthographic ? transform.up : transform.forward);
  92. state = 2;
  93. StartSound.Stop();
  94. StopSound.Play();
  95. ConstantSound.Stop();
  96. }
  97. return true;
  98. }
  99. /// <summary>
  100. /// Convenience method to turn lightsabre on / off from Unity GUI
  101. /// </summary>
  102. /// <param name="value">Value</param>
  103. public void TurnOnGUI(bool value)
  104. {
  105. TurnOn(value);
  106. }
  107. }
  108. }