- Home /
How to make a light flicker
is there a code i can use to make a light flicker on and off quickly
Answer by duck · Nov 20, 2010 at 04:21 PM
Try this script. Place it on the light gameobject.
It has two public variables that you can set in the inspector, timeOn and timeOff. They allow you to specify different amounts of time for the light to be on and off. The default is 1/10th of a second on, followed by half a second off.
var timeOn = 0.1; var timeOff = 0.5; private var changeTime = 0;
void Update() { if (Time.time > changeTime) { light.enabled = !light.enabled; if (light.enabled) { changeTime = Time.time + timeOn; } else { changeTime = Time.time + timeOff; } } }
I had to change Void Update to Function Update to get it to work in js var timeOn = 0.1; var timeOff = 0.5; private var changeTime = 0;
function Update() { if (Time.time > changeTime) { light.enabled = !light.enabled; if (light.enabled) { changeTime = Time.time + timeOn; } else { changeTime = Time.time + timeOff; } } }
Answer by Uriel_96 · Nov 20, 2010 at 04:33 PM
try this one if you want to make something like an eye to close and open it: http://unity3d.com/support/documentation/ScriptReference/Light-spotAngle.html put the minangle at 0 and the maxangle how you want and also the interval make it faster or slower.
Answer by xmediagrafx · Nov 08, 2011 at 06:25 PM
i use this....applied to your directional light.
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 ReflexGames · Sep 09, 2013 at 05:26 AM
Like this
var flickerSpeed : float = 0.1
while (true){
Light.enabled = true; Yield WaitForSeconds (flickerSpeed); } Light.enabled = false; Yield WaitForSeconds (flickerSpeed);
Answer by RasteRayzeR · Mar 24, 2014 at 06:05 PM
If you want something you can tweak a bit more, maybe you can try this solution I came up with to make a cheap lightning effect. You can tweak the flickering level by changing the size of the array (smaller means more flickering, larger means a smoother variation). This may not be optimal but does a fine job.
using UnityEngine;
using System.Collections;
public class Ambiance : MonoBehaviour {
// Your light gameObject here.
public Light light;
// Array of random values for the intensity.
private float[] smoothing = new float[20];
void Start(){
// Initialize the array.
for(int i = 0 ; i < smoothing.Length ; i++){
smoothing[i] = .0f;
}
}
void Update () {
float sum = .0f;
// Shift values in the table so that the new one is at the
// end and the older one is deleted.
for(int i = 1 ; i < smoothing.Length ; i++)
{
smoothing[i-1] = smoothing[i];
sum+= smoothing[i-1];
}
// Add the new value at the end of the array.
smoothing[smoothing.Length -1] = Random.value;
sum+= smoothing[smoothing.Length -1];
// Compute the average of the array and assign it to the
// light intensity.
light.intensity = sum / smoothing.Length;
}
}
A simple and effective solution. Works wonderfully and the effect is quite perfect for the burning torch that I am using it for.
However, I had an issue with your script not finding the Light-component when I dragged it in using the Editor.
It worked when I initialized the light-variabile in the Awake-$$anonymous$$ethod using GetComponent()
How can i edit this so that it doesn't stop at around .5, because that is just a little to dark for me.
You can set a Random.Range($$anonymous$$, max); to get the floats between thos two values.
example smoothing[smoothing.Length -1] = Random.Range(.5f, 20.0f);
Your answer
Follow this Question
Related Questions
I can't use 2D lights 2 Answers
How to I update multiple lights at once? 1 Answer
Spotlight shines out of range 1 Answer
Exclude certain objects from getting lit 2 Answers
How do I change a spot light to a point light by pressing a key? 1 Answer