Skip to content

Instantly share code, notes, and snippets.

@doches
Created September 16, 2019 11:42
Show Gist options
  • Save doches/2b78a8d8b78be876dddefaadd7e5d267 to your computer and use it in GitHub Desktop.
Save doches/2b78a8d8b78be876dddefaadd7e5d267 to your computer and use it in GitHub Desktop.
using UnityEngine;
// Usage: add to a light, specify 6-10 intervals. For a rapidly flickering light, use very short
// intervals (e.g. 0.01 - 0.3); for a horror-esque feel, use a combination of long and short intervals,
// e.g. [2.1, 0.1, 0.1, 0.2, 0.1, 0.2] for a light that flickers every 2-ish seconds.
public class LightFlicker : MonoBehaviour
{
public float[] intervals = new float[1];
private Light light;
private float elapsed = 0f;
private int index = 0;
private void Start() {
this.light = GetComponent<Light>();
}
// Update is called once per frame
void Update()
{
elapsed += Time.deltaTime;
if (elapsed > intervals[index]) {
elapsed -= intervals[index];
light.enabled = !light.enabled;
index++;
if (index >= intervals.Length) {
index = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment