Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity simple & fast light flicker script
using UnityEngine;
using System.Collections.Generic;
// Written by Steve Streeting 2017
// License: CC0 Public Domain http://creativecommons.org/publicdomain/zero/1.0/
/// <summary>
/// Component which will flicker a linked light while active by changing its
/// intensity between the min and max values given. The flickering can be
/// sharp or smoothed depending on the value of the smoothing parameter.
///
/// Just activate / deactivate this component as usual to pause / resume flicker
/// </summary>
public class LightFlickerEffect : MonoBehaviour {
[Tooltip("External light to flicker; you can leave this null if you attach script to a light")]
public new Light light;
[Tooltip("Minimum random light intensity")]
public float minIntensity = 0f;
[Tooltip("Maximum random light intensity")]
public float maxIntensity = 1f;
[Tooltip("How much to smooth out the randomness; lower values = sparks, higher = lantern")]
[Range(1, 50)]
public int smoothing = 5;
// Continuous average calculation via FIFO queue
// Saves us iterating every time we update, we just change by the delta
Queue<float> smoothQueue;
float lastSum = 0;
/// <summary>
/// Reset the randomness and start again. You usually don't need to call
/// this, deactivating/reactivating is usually fine but if you want a strict
/// restart you can do.
/// </summary>
public void Reset() {
smoothQueue.Clear();
lastSum = 0;
}
void Start() {
smoothQueue = new Queue<float>(smoothing);
// External or internal light?
if (light == null) {
light = GetComponent<Light>();
}
}
void Update() {
if (light == null)
return;
// pop off an item if too big
while (smoothQueue.Count >= smoothing) {
lastSum -= smoothQueue.Dequeue();
}
// Generate random new item, calculate new average
float newVal = Random.Range(minIntensity, maxIntensity);
smoothQueue.Enqueue(newVal);
lastSum += newVal;
// Calculate new smoothed average
light.intensity = lastSum / (float)smoothQueue.Count;
}
}
@paulrayrussell
Copy link

paulrayrussell commented May 20, 2019

Thanks, Steve. Perfectly implemented!

@arthurmarquis
Copy link

arthurmarquis commented Jun 25, 2019

Mind if I add slight movement to the light itself to add even more realism?

@sinbad
Copy link
Author

sinbad commented Jun 26, 2019

Mind if I add slight movement to the light itself to add even more realism?

Feel free to fork it to do whatever you want :)

@pourya0111
Copy link

pourya0111 commented Apr 26, 2020

Hello
How to add a emision value with independent min max intensity but the same flicker amount?
I mean light and a emission source (for example the lamp source itself in 3d world)

@arthurmarquis
Copy link

arthurmarquis commented Apr 26, 2020

@arthurmarquis
Copy link

arthurmarquis commented Apr 27, 2020

@pourya0111
Copy link

pourya0111 commented Apr 27, 2020

Just think of a parking sign that will flicker (maybe emission?) like the light

@ziplock9000
Copy link

ziplock9000 commented Dec 3, 2020

While this does work, it completely tanks performance changing the lighting on every frame, especially if several are used.

@RockyGitHub
Copy link

RockyGitHub commented Apr 1, 2021

This is awesome! Thank you so much :) It's perfect to apply to other things beyond just light.luminence too

@Angryman80
Copy link

Angryman80 commented Apr 21, 2022

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment