Better way to enable/disabled looping particle system?

Hello,

I’ve been playing with enabling and disabling a particle system by the holding or release of a button press. I have a question.

Question:
Is there a more elegant/efficient way to enable and disable a particle system than this?

    /*************************************************************************************************
    *   Important Note
    *       The emission module is "emission" category on the Particle System component
    *       We are checking the boolean true/false to turn this on and off
    *       In play mode you can watch the checkbox by emission come on and off as you press "Jump"
    *************************************************************************************************/

    private ParticleSystem ps;                  // Variable to refer to your particle system

    void Start()
    {
        ps = GetComponent<ParticleSystem>();    // Get the particle system on your GameObject once
        var emission = ps.emission;                 // Get the current emission module
        Debug.Log(emission.GetType().FullName);
        emission.enabled = false;                   // Set the emission module to disabled at start
    }

    void Update()
    {
        if (Input.GetButtonDown("Jump")){       // The one frame when you press the jump key, turn it on
            var emission = ps.emission;             // Get the current emission module
            emission.enabled = true;                // Set the emission module to enabled
        }                                      
        if (Input.GetButtonUp("Jump")){         // The one frame when you let go of the jump key, turn it off
            var emission = ps.emission;             // Get the current emission module
            emission.enabled = false;               // Set the emission module to disabled
        }
    }
  1. I want to create the emission variable globally for access in all the methods (Start/Update) for future use rather than using “var emission = ps.emission” every time I want to enable/disable.
  2. There doesn’t seem to be a class/dataType that I can use though. The data type that typeOf() returns is ParticleSystem+emissionModule

Thank you,
Brendan

Instead of enabling/disabling the emission system, you can just tell the ParticleSystem to start and stop. By default, it will clear it’s existing particles, but you can send an extra parameter to have it stop emitting while allowing the existing particles to continue. If you want to control ParticleSystems with Start and Stop, I suggest setting them not to “play on awake” in-editor.

// Stop emission and clear particles
particleSystem.Stop(true);

// Stop emission but don't clear particles
particleSystem.Stop(true, ParticleSystemStopBehavior.StopEmitting);
12 Likes

Thank you so much, this was super helpful.

One thing is you switched the default. Default is to leave particles. If you want them to clear, you need to pass in the extra parameter. Below is a sample I made.

    private ParticleSystem ps;                  // Variable to refer to your particle system

    void Start()
    {
        ps = GetComponent<ParticleSystem>();    // Get the particle system on your GameObject once
    }

    void Update()
    {
        if (Input.GetButtonDown("Jump")){       // The one frame when you press the jump key
            ps.Play();                              // Start emitting particles (playing on a loop)
        }                                      
        if (Input.GetButtonUp("Jump")){         // The one frame when you let go of the jump key
            ps.Stop();                              // Stop emitting new particles, but leave already emitted particles in scene
         }
    }

    /****************************************************************************************************************************
    *   If you want to stop emitting AND clear all previously emitted particles, use the command below
    *       ps.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
    *
    *   The first parameter is a boolean saying whether or not to stop emitting on all particle system on child GameObjects
    *   The second parameter takes a stop behavior and only has two options:
    *       ParticleSystemStopBehavior.StopEmitting  (default - leaves already emitted particles)
    *       ParticleSystemStopBehavior.StopEmittingAndClear  (CameraClearFlags all particles from the system when it stops)
    ****************************************************************************************************************************/
2 Likes