Home Unity

Flickering Lights:

polycounter lvl 18
Offline / Send Message
Vailias polycounter lvl 18
Inspired by a thread in the UDK subforum here, I put together this little script to mimic a random flickering light like a nearly dead florescent tube. Looks pretty convincing with default values. It flickers about as much as it is steady. (Gif captures WAY more flicker than at full speed over time.. plus it doesn't loop in real life. But better than nothin)
FlickerLight.Gif
using UnityEngine;
using System.Collections;

public class FlickerLight : MonoBehaviour {

	public float fullIntensity = 1f;
	public float flickerPeriod = 1f;
	public float steadyPeriod = 2f;
	
	
	// Update is called once per frame
	void Update () {

	float currentIntensity = 0f;

	currentIntensity = Mathf.Max (Mathf.Ceil(Mathf.Sin(Time.time/steadyPeriod)),Mathf.Ceil (Mathf.Sin (Time.time/(steadyPeriod*0.333333F))));
	//Creating an irregular on period by max of square waves with asynchronous period.

	light.intensity = Mathf.Max (currentIntensity, flicker(Time.time/flickerPeriod))*fullIntensity;
	}
	
	private float flicker(float flickerIn)
	{
		flickerIn = flickerIn - Mathf.Floor(flickerIn); //drop the real part of the number
		flickerIn = (Mathf.Sin(3.0f/flickerIn)*Mathf.Sin(5f/(1-flickerIn)));
		return flickerIn;
		
	}
}

Replies

  • EvilPixills
    Offline / Send Message
    EvilPixills polygon
    Sweet, thanks for the share! I'm sure this will come in handy somewhere down the road.
  • Vailias
    Offline / Send Message
    Vailias polycounter lvl 18
    Poked at this a bit more the other night as the flicker needed to be a bit more natural and less regular. Modifying how the square wave periods interacted worked nicely.
    I also included a random seed value so you can either sync or unsync the flicker behaviour between multiple lights by using the same seed value. (ie if you use 3-4 lights in a line as a single fluorescent source they should all flash at once, but multiple fluorescent sources shouldn't all flicker at the same time) The randomization is limited to less than 15 time the base steady period so the offset doesn't just go crazy.

    using UnityEngine;
    using System.Collections;
    
    public class FlickerLight : MonoBehaviour {
    
    	public float fullIntensity = 1f;
    	public float flickerPeriod = 1f;
    	public float steadyPeriod = 2f;
    	public int randomSeed  = 128;
    	private System.Random randomizer; 
    	private float offset = 0f;
    	
    	void Start()
    	{
    		randomizer = new System.Random(randomSeed);
    		offset = randomizer.Next((System.Int32)steadyPeriod*15);
    	}
    
    	void Update () {
    
    	float currentIntensity = 0f;
    	float cycleTime = Time.time + offset;
    	currentIntensity = Mathf.Max (Mathf.Ceil(Mathf.Sin(cycleTime/steadyPeriod)),Mathf.Ceil (Mathf.Sin (cycleTime/(2*steadyPeriod*0.333333F))));
    	//Creating an irregular on period by max of square waves with asynchronous period.
    
    	light.intensity = Mathf.Max (currentIntensity, flicker(cycleTime/flickerPeriod))*fullIntensity;
    	}
    	
    	private float flicker(float flickerIn)
    	{
    		flickerIn = flickerIn - Mathf.Floor(flickerIn); //drop the real part of the number
    		flickerIn = 0.5f*(1f+(Mathf.Sin(3.0f/flickerIn)*Mathf.Sin(5f/(1-flickerIn))));
    		return flickerIn;
    		
    	}
    
    
    
  • spectre1130
    Offline / Send Message
    spectre1130 polycounter lvl 6
    I was going to do a flickering light in my latest project. So Unity doesn't have any built-in functioning for this like UDK's Kismet? Anyhow, nice work. It looks cool.
  • Vailias
    Offline / Send Message
    Vailias polycounter lvl 18
    You mean some node based scripting thing?
    no.

    Kismet has its uses, but far too often people try and force it to be unrealScript, when its not really built for the level of control Uscript actually has.

    Also its just C# man. Combined with Monodevelop's intellisense/autocomplete thing. Its stupendously easy to learn. (though honestly the difference between UnrealScript and C# isn't far off at this point) Also Unity has provided some really quite good tutorials on scripting with C#.

    Unity does update super fast though, so unlike UDK you don't have to cook your scripts to try them out.

    Glad you found it helpful.
Sign In or Register to comment.