Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Collegeboy95 · Oct 21, 2017 at 04:43 AM · monodeveloplightningthunder

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;

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
                 }
             }
 
         }
 
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rickywild · Oct 22, 2017 at 11:35 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Delay on thunderlight 3 Answers

Does MonoDeveloper support language other than English 0 Answers

MonoDevelop slowed down my project's start time? 1 Answer

Why my solution has 2 entries? How to get rid of one of those? 1 Answer

Why is the 'Run' button in Monodevelop is grayed out? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges