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)
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
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.
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.