Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 nursedayuksel · Feb 25, 2021 at 11:43 AM · particlesparticle systemfog

How can I activate fog after lightning strikes?

Good day peeps!

In the game I'm working on, I want there to be a feature in which once a really big lightning strikes, the electricity will be cut off. I'm simulating the dark, 'no electricity' environment by using a black fog. (Might come up with something better later, for now I'm just playing around).

How can I check if the lightning stroke? I'm planning on using a particle system to create a lightning (from this specific tutorial: https://www.youtube.com/watch?v=ewC_c6aHbf8&t=1s).

I'm sure there are multiple ways of achieving this. One of the ideas I thought of would be to check if the lightning collides with the ground, and when it does I will activate the fog. Would that be an efficient way to tackle this problem?

Any help is appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Llama_w_2Ls · Feb 25, 2021 at 11:59 AM

One thing you might want to look into is particle system callbacks. When a particle system ends, you can decide to either destroy the particle system, or call a method. In the Stop Action dropdown of the particle system settings, set it to callback then assign a script that contains a method. This method is OnParticleSystemStopped and you can attach a script to the particle system that calls this method when finished.


Finally, when the particle system stops, as in the lightning struck the ground, you could enable the fog, using RenderSettings.Fog = true;. I don't know how your particle system works, but if the lighting happens on a regular interval, you could use a timer that calls that method whenever the lighting is expected to strike.

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 nursedayuksel · Feb 25, 2021 at 01:48 PM 0
Share

Thank you thank you!!! I started working with particle systems for the first time this week so I had no idea about that method! Works like a charm~

avatar image
0

Answer by nursedayuksel · Feb 25, 2021 at 01:54 PM

In case anyone is curious about how I achieved this (thanks to Llama_w_2Ls's suggestion as well as Unity's documentation about OnParticleSystemStopped), I was able to get it to work with the following code:

 public class LightningStriking : MonoBehaviour
 {
     public AudioClip lightningSound; // audio clip for lightning sound
     public AudioSource lightningAudioSource; // reference to the lightning audio source
 
     public AudioClip powerOutage; // audio clip for power outage sound
 
     private GameObject[] lights;  // array for referencing all the lights in the scene
 
     void Start()
     {
         // these two lines aren't needed if you already set the Stop Action to Callback from the inspector
         var main = GetComponent<ParticleSystem>().main;
         main.stopAction = ParticleSystemStopAction.Callback;
 
         lights = GameObject.FindGameObjectsWithTag("Lights");
     }
 
     void OnParticleSystemStopped()
     {
         // Debug.Log("Oh no! Lightning Stroke!");
 
         StartCoroutine("SoundFX");

         // activate fog
         RenderSettings.fog = true;
         RenderSettings.fogMode = FogMode.ExponentialSquared;
         RenderSettings.fogColor = new Color(0, 0, 0);
         RenderSettings.fogDensity = 1f;
 
         // for disabling the lights in the scene
         foreach (GameObject light in lights)
         {
             lightningAudioSource.PlayOneShot(powerOutage);
             light.SetActive(false);           
         }
     }
 
     IEnumerator SoundFX()
     {
         yield return new WaitForSeconds(Random.Range(0f, 0.75f));
         lightningAudioSource.PlayOneShot(lightningSound); // play the lightning sound
     }
 }

Might not be perfect, but it does the job for me!

Comment
Add comment · Show 2 · 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 Llama_w_2Ls · Feb 25, 2021 at 02:22 PM 0
Share

One thing to add. You can do this section of the code:

          var main = GetComponent<ParticleSystem>().main;
          main.stopAction = ParticleSystemStopAction.Callback;

In the inspector for the particle system. In the main settings, there's a dropdown called Stop Action. You can set it to callback there, instead of doing it through code. Anyways, glad I helped.

avatar image nursedayuksel Llama_w_2Ls · Feb 25, 2021 at 02:25 PM 0
Share

You're right about that, thank you! I forgot to remove it from the code after setting it to callback through the inspector. :D

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

118 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 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

Rain particles are getting affected by the fog 2 Answers

Particle System: How To Control Axis? 0 Answers

Use of particles with built in fog 0 Answers

Particles collision Stopped working after a while 0 Answers

Trying to change a particle system on and off with a toggle 0 Answers


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