Created
July 10, 2016 17:38
-
-
Save ted80/4a1499bae29ec7535be60d223d620fca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class StreetLight : MonoBehaviour | |
{ | |
private bool state; | |
private bool blink; | |
private float stateTime; | |
private float blinkTime; | |
private float saveTime; | |
public Light light; | |
private float defaultIntensity; | |
void Start () | |
{ | |
state = true; | |
blink = false; | |
saveTime = 1f; | |
defaultIntensity = light.intensity; | |
updateLight(true); | |
} | |
void FixedUpdate () | |
{ | |
stateTime += Time.deltaTime; | |
if(state) | |
{ | |
if((Random.value * 50f) + saveTime < stateTime) | |
{ | |
state = false; | |
blink = true; | |
stateTime = 0f; | |
blinkTime = 0f; | |
updateLight(false); | |
} | |
} | |
else | |
{ | |
if((Random.value * 2f) + 0.3f < stateTime) | |
{ | |
state = true; | |
blink = false; | |
stateTime = 0f; | |
saveTime = 0.5f + Random.value * 3f; | |
updateLight(true); | |
} | |
else | |
{ | |
blinkTime += Time.deltaTime; | |
if((Random.value * 0.2f) < blinkTime) | |
{ | |
blink = !blink; | |
updateLight(!blink); | |
blinkTime = 0f; | |
} | |
} | |
} | |
} | |
private void updateLight(bool _state) | |
{ | |
light.intensity = _state ? defaultIntensity : 0f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment