- Home /
Lightning flash
I need a full script that can flash a light at random intervals for lightning simple as that
Answer by DaveA · Jan 15, 2011 at 01:13 AM
This is a real quick idea, you'll want to play with the numbers, but you should get the ideas. Attach to a DirectionalLight I would think.
var minTime = .5; var thresh = .5;
private var lastTime = 0; private var myLight;
function Start() { myLight = GetComponent(Light); }
function Update () {
if ((Time.time - lastTime) > minTime)
if (Random.value > thresh)
light.enabled = true;
else
light.enabled = false;
lastTime = Time.time;
}
thresh is the minimum threshold between 0 and 1 that a random number must exceed to turn the light on. minTime is the minimum time interval to check that random number.
A more realistic effect might have a 'strike check' time (between 'strikes') and each strike would be 'on' for some small amount of time, during which rapid flashing would take place. I'll leave that to you.
Here are some more ideas: http://answers.unity3d.com/questions/14505/lightning-with-random-intervals
How memory intensive, is this approach? As in toggling enabling this fast, this often? And creds for provindg a full script to the poster.
STARS - use a single manager script to handle the ti$$anonymous$$g and tell the lights that subscribe to the change to change their enabledness when they are told to by the manager
STARS - make myLight an array of lights and assign the lights that need to be on/off at the same time to the array. Then when turning on/off, iterate through the array setting 'enabled' for all of them.
Answer by dazzsnow · Oct 25, 2013 at 04:20 AM
Here is a java script for Lightning and Thunder.
public var offMin : float= 10;
public var offMax : float= 60;
public var onMin : float=0.25;
public var onMax : float= 0.8;
public var l : Light;
public var Thunder : AudioClip[];
function Start()
{
light();
}
function light()
{
while(true)
{
yield WaitForSeconds(Random.Range(offMin, offMax));
l.enabled = true;
soundfx();
yield WaitForSeconds(Random.Range(onMin, onMax));
l.enabled = false;
}
}
function soundfx()
{
yield WaitForSeconds(Random.Range(0.25,1.75));
audio.PlayOneShot(Thunder[Random.Range(0,Thunder.Length)]);
}
Your answer
Follow this Question
Related Questions
Flashes of light 0 Answers
How do i spawn a square in random position within an area but only at certain x and y intervals? 1 Answer
Random Array of sounds for Random Game Object 1 Answer
Adding a collider to a lightning script that has a constantly changing size 0 Answers
My gameObject starts flashing 0 Answers