Skip to content

Instantly share code, notes, and snippets.

@ted80
Created July 10, 2016 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ted80/4a1499bae29ec7535be60d223d620fca to your computer and use it in GitHub Desktop.
Save ted80/4a1499bae29ec7535be60d223d620fca to your computer and use it in GitHub Desktop.
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