Does anyone have an idea about how to apply a flicker effect to a light?
Any help would be much appreciated
Cheers
Here try this:
var flickerSpeed : float = 0.1;
while (true) {
light.enabled = true;
yield WaitForSeconds (flickerSpeed);
light.enabled = false;
yield WaitForSeconds (flickerSpeed);
}
This would probably be more realistic
var minFlickerSpeed : float = 0.1;
var maxFlickerSpeed : float = 1.0;
function Update()
{
light.enabled = true;
yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed );
light.enabled = false;
yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed );
}
This one should work
var flickerSpeed : float = 0.07;
private var randomizer : int = 0;
while (true) {
if (randomizer == 0) {
light.enabled = false;
}
else light.enabled = true;
randomizer = Random.Range (0, 1.1);
yield WaitForSeconds (flickerSpeed);
}
thanks guys
im a little intrigued to know why you guys are posting on the forum at 3 in the morning though!?!
i have a valid excuse, i have a deadline to meet tomorrow!
cheers
I’m in California, where it’s about 7:30 PM. Where are you?
ahhh i see that would make sense!
im in good old england, stuck in a studio at my university, totally shattered
cheers for your help though, its very much appreciated
You could use the following script to animate your light in various time based ways. If you use “noise” the flickering will be randomized. All other functions will increse and decrease the light color with the specified parameters.
// Properties
var waveFunction : String = "sin"; // possible values: sin, tri(angle), sqr(square), saw(tooth), inv(verted sawtooth), noise (random)
var base : float = 0.0; // start
var amplitude : float = 1.0; // amplitude of the wave
var phase : float = 0.0; // start point inside on wave cycle
var frequency : float = 0.5; // cycle frequency per second
// Keep a copy of the original color
private var originalColor : Color;
// Store the original color
function Start () {
originalColor = GetComponent(Light).color;
}
function Update () {
var light : Light = GetComponent(Light);
light.color = originalColor * (EvalWave());
}
function EvalWave () {
var x : float = (Time.time + phase)*frequency;
var y : float;
x = x - Mathf.Floor(x); // normalized value (0..1)
if (waveFunction=="sin") {
y = Mathf.Sin(x*2*Mathf.PI);
}
else if (waveFunction=="tri") {
if (x < 0.5)
y = 4.0 * x - 1.0;
else
y = -4.0 * x + 3.0;
}
else if (waveFunction=="sqr") {
if (x < 0.5)
y = 1.0;
else
y = -1.0;
}
else if (waveFunction=="saw") {
y = x;
}
else if (waveFunction=="inv") {
y = 1.0 - x;
}
else if (waveFunction=="noise") {
y = 1 - (Random.value*2);
}
else {
y = 1.0;
}
return (y*amplitude)+base;
}
[/code]
Harry, in your script what part of the script changes the brightness of the light?
thanks
Daniel the light color computation is done in the update function. Since color is a vector (rgba) you can use scalar multiplication to fade from original color (scalar value=1.0) to black (scalar value is 0.0).
This is the reason why we have to store the original color value (which is done in the Start function).
EvalWave() delivers the amplitude (plus the base value) of the given wave at the current point of time.
Just put the code into a script and attach it to a light source and you’ll see it work.
// light reference
var light : Light = GetComponent(Light);
// this part changes the light via vector multiplication
light.color = originalColor * (EvalWave());
Daniel the light color computation is done in the update function. Since color is a vector (rgba) you can use scalar multiplication to fade from original color (scalar value=1.0) to black (scalar value is 0.0).
This is the reason why we have to store the original color value (which is done in the Start function).
EvalWave() delivers the amplitude (plus the base value) of the given wave at the current point of time.
Just put the code into a script and attach it to a light source and you’ll see it work.
// light reference
var light : Light = GetComponent(Light);
// this part changes the light via vector multiplication
light.color = originalColor * (EvalWave());
Okay Thanks. Okay Thanks.
I have a strange problem when using the light flicker script and it appears only when played in fullscreen. In the editor or when played windowed it works great. In fullscreen the whole screen suddenly shows bold horizontal lines, looks like an overlay problem.
Could someone try this out in fullscreen?
Wow. Talk about an old thread. There are a number of scripts presented in this thread, which one did you try to use?
This one:
// Properties
var waveFunction : String = "sin"; // possible values: sin, tri(angle), sqr(square), saw(tooth), inv(verted sawtooth), noise (random)
var base : float = 0.0; // start
var amplitude : float = 1.0; // amplitude of the wave
var phase : float = 0.0; // start point inside on wave cycle
var frequency : float = 0.5; // cycle frequency per second
// Keep a copy of the original color
private var originalColor : Color;
// Store the original color
function Start () {
originalColor = GetComponent(Light).color;
}
function Update () {
var light : Light = GetComponent(Light);
light.color = originalColor * (EvalWave());
}
function EvalWave () {
var x : float = (Time.time + phase)*frequency;
var y : float;
x = x - Mathf.Floor(x); // normalized value (0..1)
if (waveFunction=="sin") {
y = Mathf.Sin(x*2*Mathf.PI);
}
else if (waveFunction=="tri") {
if (x < 0.5)
y = 4.0 * x - 1.0;
else
y = -4.0 * x + 3.0;
}
else if (waveFunction=="sqr") {
if (x < 0.5)
y = 1.0;
else
y = -1.0;
}
else if (waveFunction=="saw") {
y = x;
}
else if (waveFunction=="inv") {
y = 1.0 - x;
}
else if (waveFunction=="noise") {
y = 1 - (Random.value*2);
}
else {
y = 1.0;
}
return (y*amplitude)+base;
}
When I force V-Synch in the project settings, it works fine, even in fullscreen.
The script shouldn’t cause the effect but try to use LateUpdate() instead of Update(). If this doesn’t help check your graphic card driver since you mention that v-sync prevents that problem.
I tried LateUpdate, but still the same effect. I even tested it on 4 different systems, Nvidia and ATI based. This is so strange, it appears always only on Fullscreen and without forcing V-Synch.
What’s funny is that I’ve used a different script that goes for the same flicker effect but takes a slightly different approach and the result was the same error. I kinda looks like tearing but all over the place.
Except the vsync solution - which normally should be turned on by default because it doesn’t make sense to render more frames than your monitor can display and cause these artifacts by showing 2 rendered images during the same screen refresh - you should check whether the are any z-buffer conflicts.
Also check the flicker intervall wich should not be smaller than the monitor frequency (1000ms/60Hz = 16.7ms)
Except the vsync solution - which normally should be turned on by default because it doesn’t make sense to render more frames than your monitor can display and cause these artifacts by showing 2 rendered images during the same screen refresh - you should check whether the are any z-buffer conflicts.
Also check the flicker intervall wich should not be smaller than the monitor frequency (1000ms/60Hz = 16.7ms)
a slight mod to the above:
var minFlickerSpeed : float = 0.01;
var maxFlickerSpeed : float = 0.1;
var minLightIntensity : float = 0;
var maxLightIntensity : float = 1;
while (true)
{
light.enabled = true;
light.intensity = Random.Range(minLightIntensity, maxLightIntensity);
yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
light.enabled = false;
yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
}