- Home /
Lightning and Thunder Script System
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: https://youtu.be/nMoAu0FRfus?t=2m30s
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;
}
Answer by Rickywild · Oct 22, 2017 at 11:30 AM
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[i].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;
}
}
}
}
}
Additionally, if you've read through the code above and understand it and assu$$anonymous$$g you'd like to use it. You'll also need this, hope it helps you out
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightningBolt : $$anonymous$$onoBehaviour
{
[Header("FLAG FLASH")]
public bool flag;
private SpriteRenderer spriteRenderer;
private float intensity;
private float maxFadedAlpha;
private Color colour;
private float alpha;
private bool fadedOut, fadedIn;
void Start ()
{
spriteRenderer = this.gameObject.GetComponent<SpriteRenderer>();
intensity = 0.05f;
maxFadedAlpha = 0.01f;
alpha = 1f;
colour = new Color(1f, 1f, 1f, alpha);
fadedOut = true;
fadedIn = false;
flag = false;
}
private void FadeProcess(bool fadeIn)
{
if (fadeIn)
{
if (alpha <= 0.9999f)
alpha += intensity;
else
{
fadedIn = true;
}
}
if (!fadeIn)
{
if (alpha >= maxFadedAlpha)
alpha -= intensity;
else
{
fadedOut = true;
}
}
colour.a = alpha;
//this.GetComponent<SpriteRenderer>().color = colour;
spriteRenderer.color = colour;
//renderer.material.color = colour;
}
void Update ()
{
if (flag)
{
//Fade back in, player is out of doorway.
if(!fadedIn)
FadeProcess(true);
else
{
if (!fadedOut)
FadeProcess(false);
else
{
//automatically disable for later use in StormLightning.cs
//this.gameObject.SetActive(false);
}
}
//Reset this value for use again.
if (fadedOut)
fadedOut = false;
}
}
}
Your answer