grimmy
1
I want my light to flicker at a fixed rate but the following code makes the light turn on/off at random intervals…I really cant undertsand why???
function Update () {
DoFlicker();
}
function DoFlicker()
{
print("max intensity");
myLight.intensity=maxIntensity;
yield WaitForSeconds(flickerDelay*=Time.deltaTime);
print("min intensity");
myLight.intensity=minIntensity;
yield WaitForSeconds(flickerDelay*=Time.deltaTime);
}
grimmy
2
Hmm I just ried the following which works fine. So whats the difference??
function Start()
{
DoFlicker();
}
function Update() {
}
function DoFlicker()
{
//print("max intensity");
while(true)
{
myLight.intensity=maxIntensity;
yield WaitForSeconds(flickerDelay);
//print("min intensity");
myLight.intensity=minIntensity;
yield WaitForSeconds(flickerDelay);
}
}
You can use this script:
using UnityEngine;
using System.Collections;
[AddComponentMenu("Tamer/General/lightFlicker")]
[RequireComponent(typeof(Light))]
public class lightFlicker : MonoBehaviour {
public float interval = 0.7F;
private Color col;
private float flick = 0.0F;
private LightShadows shadow;
void Start () {
col = light.color;
shadow = light.shadows;
}
void Update () {
if (Random.value >= interval) {
flick = Random.value;
light.color = new Color(col.r * flick, col.g * flick, col.b * flick);
if (shadow != LightShadows.None) light.shadowStrength = flick;
}
}
}
If you are not on pro, just delete the shadow parts.