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: Unity 2017 - Game VFX -Lightning and Thunder Effect - YouTube).

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!

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.

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!