Hey, I don’t know how to make a Lightning script. I want to make the lightning flash the screen with the lightning bolt before a thunderclap sound is play afterwards. How can I do that?
Here’s my Lightning script I used from YouTube for this:
#pragma strict
var minTime = 0.5;
var threshold = 0.5;
var myLight : Light;
private var lastTime = 0;
function Update () {
if((Time.time - lastTime) > minTime)
if (Random.value > threshold)
GetComponent.<Light>().enabled = true;
else
GetComponent.<Light>().enabled = false;
lastTime = Time.time;
}
Hey there, I read this and thought i’d drop my solution here. It’s not the best script, certainly could have been done better but it was a quick fix for what I needed and I have little time to spend perfecting it. I’m not going to explain it either, hope the code is useful regardless, good luck!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StormLightning : MonoBehaviour
{
[Header("TIME FOR LIGHT INCREASE")]
public float time_lightening;
[Header("TIME FOR LIGHT DECREASE")]
public float time_darkening;
[Header("LIGHT INTENSITY")]
public float intensity;
[Header("TIME UNTIL NEXT FLASH")]
public float wait_time;
[Header("RANDOMIZE FLASHES")]
public bool flashIsRand;
[Header("LIGHTNINGBOLT SPRITES")]
public GameObject[] lightningBolts;
private Light myLight;
private float timeA_stamp, timeB_stamp, timeC_stamp, rand_wait;
private bool timeA_stamped, timeB_stamped, timeC_stamped;
private int rand_sfx, rand_bolt, prevBolt;
void Start ()
{
myLight = this.GetComponent<Light>();
intensity = 0.3f;
timeA_stamp = 0f;
timeB_stamp = 0f;
timeC_stamp = 0f;
timeA_stamped = false;
timeB_stamped = false;
timeC_stamped = false;
rand_wait = 0f;
rand_sfx = 0;
prevBolt = 0;
rand_bolt = 0;
if(lightningBolts != null)
{
for(int i = 0; i < lightningBolts.Length; i++)
{
lightningBolts*.SetActive(false);*
-
}*
-
}*
-
}*
-
void Update ()*
-
{*
-
if(!timeC_stamped)*
-
{*
-
if (!timeA_stamped)*
-
{*
-
if(!flashIsRand)*
-
AudioManager.RunEnviroSFX(5, false);*
-
else*
-
{*
-
rand_sfx = (int)UnityEngine.Random.Range(5, 8);*
-
AudioManager.RunEnviroSFX(rand_sfx, false);*
-
if (lightningBolts != null)*
-
{*
-
//Don't forget to disable the previous lightning bolt used.*
-
if(lightningBolts[prevBolt].activeSelf)*
-
{*
-
lightningBolts[prevBolt].SetActive(false);*
-
}*
-
//Pick and set a lightning bolt sprite to display.*
-
//It's attached script should render it invisible after*
-
//a short display.*
-
rand_bolt = (int)UnityEngine.Random.Range(0, 9);*
-
prevBolt = rand_bolt;*
-
lightningBolts[rand_bolt].SetActive(true);*
-
lightningBolts[rand_bolt].GetComponent<LightningBolt>().flag = true;*
-
}*
-
}*
-
timeA_stamp = Time.time;*
-
timeA_stamped = true;*
-
}*
-
else*
-
{*
-
//Create a fast increase in light intensity here.*
-
if (!timeB_stamped)*
-
{*
-
if (Time.time < timeA_stamp + time_lightening)*
-
{*
_ myLight.intensity += intensity * Time.timeScale;_
-
}*
-
else*
-
{*
-
if (lightningBolts != null)*
-
lightningBolts[rand_bolt].SetActive(false);*
-
timeB_stamp = Time.time;*
-
timeB_stamped = true;*
-
}*
-
}*
-
}*
-
if (timeB_stamped)*
-
{*
-
//Here we want the light to descrease fast.*
-
if (Time.time < timeB_stamp + time_darkening)*
-
{*
_ myLight.intensity -= intensity * Time.timeScale;_
-
}*
-
else*
-
{*
-
//Now we stamp the time as one lightning flash has occured.*
-
//Only do this process again after the specidied wait time*
-
//has passed.*
-
if (!timeC_stamped)*
-
{*
-
//if(lightningBolts != null)*
-
// lightningBolts[rand_bolt].SetActive(false);*
-
timeC_stamp = Time.time;*
-
timeC_stamped = true;*
-
if(flashIsRand)*
-
{*
-
rand_wait = (float)UnityEngine.Random.Range(1, 7);*
-
//Don't forget to flag the logic of a lightningbolt display to false/disabled.*
-
lightningBolts[rand_bolt].GetComponent<LightningBolt>().flag = false;*
-
}*
-
}*
-
}*
-
}*
-
}*
-
else*
-
{*
-
//Here is the wait time before processing the logic that*
-
//creates a lightning flash.*
-
if(!flashIsRand)*
-
{*
-
if (Time.time > timeC_stamp + wait_time)*
-
{*
-
timeA_stamped = false;*
-
timeB_stamped = false;*
-
timeC_stamped = false;*
-
}*
-
}*
-
else*
-
{*
-
if (Time.time > timeC_stamp + rand_wait)*
-
{*
-
timeA_stamped = false;*
-
timeB_stamped = false;*
-
timeC_stamped = false;*
-
}*
-
}*
-
}*
-
}*
}