- Home /
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!
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.
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~
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!
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.
You're right about that, thank you! I forgot to remove it from the code after setting it to callback through the inspector. :D