Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- namespace SpartanFist
- {
- [AddComponentMenu("Interactions/Switches/Auto Switch")]
- public class AutoSwitch : Switch
- {
- /* Auto Switch Stuff */
- // If this is true, the time to the switch's first OFF/ON is randomized
- public bool randomizeInitialTime = true;
- // 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)
- public float timeOffset = 0.0f;
- // Time between turning off and turning back on again
- public float timeSpentOff = 1.0f;
- // Length of time switch is on (set to 0 for a single frame on)
- public float timeSpentOn = 0.0f;
- private float timeUntilChange = 0.0f;
- /* Standard Functions */
- public override void Awake()
- {
- base.Awake();
- if (randomizeInitialTime)
- {
- if (SwitchState)
- {
- timeUntilChange = Random.Range(0.0f, timeSpentOn);
- }
- else
- {
- timeUntilChange = Random.Range(0.0f, timeSpentOff);
- }
- }
- else
- {
- timeUntilChange = timeOffset;
- }
- }
- public override void Update()
- {
- float deltaTime = PhysicsManager.GetDeltaTimeFor(gameObject);
- timeUntilChange -= deltaTime;
- if (timeUntilChange <= 0.0f)
- {
- SwitchState = !SwitchState;
- if (SwitchState)
- {
- timeUntilChange = timeSpentOn;
- }
- else
- {
- timeUntilChange = timeSpentOff;
- }
- }
- base.Update();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment