Skip to content

Instantly share code, notes, and snippets.

@tangentstorm
Created July 13, 2015 15:24
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 tangentstorm/069ab7bbec92f98a87b0 to your computer and use it in GitHub Desktop.
Save tangentstorm/069ab7bbec92f98a87b0 to your computer and use it in GitHub Desktop.
A flickering light behavior for unity.
using UnityEngine;
using System.Collections;
public class FlickerBehavior : MonoBehaviour {
// the (comparatively) steady light phase
public float minLightMs = 500;
public float maxLightMs = 5000;
// dark part of the flicker phase:
public float minDarkMs = 25;
public float maxDarkMs = 50;
// the time between flickers in the dark phase:
public float minFlickMs = 25;
public float maxFlickMs = 50;
// number of times to flicker during the dark phase:
public int minFlicks = 1;
public int maxFlicks = 3;
// internal reference to the light component
private Light _light;
void Start () {
_light = GetComponent<Light>();
StartCoroutine("Flicker");
}
IEnumerator Flicker () {
while (true) {
// steady light period
_light.enabled = true;
yield return new WaitForSeconds(Random.Range(minLightMs, maxLightMs)/1000f);
// flickering light period
for (int i = 0; i < Random.Range (minFlicks, maxFlicks); i++) {
_light.enabled = false;
yield return new WaitForSeconds(Random.Range(minDarkMs, maxDarkMs)/1000f);
_light.enabled = true;
yield return new WaitForSeconds(Random.Range(minFlickMs, maxFlickMs)/1000f);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment