- Home /
Help with my Light Flicker Script...
This is my light flicker script. How can i modify this so light pulsates rather than flickers, sort of like glowing effect. This is simple on off, rather i need it to dim down then back up...
function Update ()
{
if ( Random.value > 0.9 ) //a random chance
{
if ( light.enabled == true ) //if the light is on...
{
light.enabled = false; //turn it off
}
else
{
light.enabled = true; //turn it on
}
}
}
Answer by JakeElliott · Nov 08, 2011 at 06:12 PM
I'm assuming that light
is a reference to a Light
component. Instead of just activating/deactivating the component, you can adjust the 'intensity' property to fade in and out.
It might be most natural to do this with a coroutine; something like this:
function Update() {
if (Random.value > 0.9 && light.intensity == 1.0) {
StartCoroutine("FlickerLight");
}
}
function FlickerLight() {
while(light.intensity > 0.0) {
light.intensity -= Time.deltaTime;
yield;
}
while(light.intensity < 1.0) {
light.intensity += Time.deltaTime;
yield;
}
light.intensity = 1.0;
}
The light.intensity = 1.0
line at the end of the FlickerLight
method is just to ensure the value ends up at 1.0 instead of going over. You can adjust the speed by multiplying Time.deltaTime by some value.
Your answer
Follow this Question
Related Questions
How can i make a static or wave effect on camera screen? 0 Answers
Flashlight effect 2 Answers
Ray Light Effect 2 Answers
Light Glare Effects 2 Answers