Guest User

Untitled

a guest
May 30th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace SpartanFist
  5. {
  6. [AddComponentMenu("Interactions/Switches/Auto Switch")]
  7. public class AutoSwitch : Switch
  8. {
  9. /* Auto Switch Stuff */
  10.  
  11. // If this is true, the time to the switch's first OFF/ON is randomized
  12. public bool randomizeInitialTime = true;
  13. // If randomize is false, this is the initial delay for the switch's first ON/OFF event (useful for setting up timed series of events)
  14. public float timeOffset = 0.0f;
  15. // Time between turning off and turning back on again
  16. public float timeSpentOff = 1.0f;
  17. // Length of time switch is on (set to 0 for a single frame on)
  18. public float timeSpentOn = 0.0f;
  19.  
  20. private float timeUntilChange = 0.0f;
  21.  
  22. /* Standard Functions */
  23.  
  24. public override void Awake()
  25. {
  26. base.Awake();
  27.  
  28. if (randomizeInitialTime)
  29. {
  30. if (SwitchState)
  31. {
  32. timeUntilChange = Random.Range(0.0f, timeSpentOn);
  33. }
  34. else
  35. {
  36. timeUntilChange = Random.Range(0.0f, timeSpentOff);
  37. }
  38. }
  39. else
  40. {
  41. timeUntilChange = timeOffset;
  42. }
  43. }
  44.  
  45. public override void Update()
  46. {
  47. float deltaTime = PhysicsManager.GetDeltaTimeFor(gameObject);
  48. timeUntilChange -= deltaTime;
  49. if (timeUntilChange <= 0.0f)
  50. {
  51. SwitchState = !SwitchState;
  52. if (SwitchState)
  53. {
  54. timeUntilChange = timeSpentOn;
  55. }
  56. else
  57. {
  58. timeUntilChange = timeSpentOff;
  59. }
  60. }
  61.  
  62. base.Update();
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment