Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I trigger a group of specific particle effects attached to a game object on collision?

Discussion in 'Scripting' started by jeremy_mccloud, Mar 10, 2020.

  1. jeremy_mccloud

    jeremy_mccloud

    Joined:
    Feb 4, 2018
    Posts:
    8
    I have a rocket ship that the player controls in my game. Attached to the rocket ship object, are two groups of particle effects. One is for the thrusts, which emits flames when the user presses the space bar, and another is an explosion, when the player runs into an enemy or wall.

    I'm a little confused on how to trigger the explosion when the ship collides into an enemy as it's a collection of several particle effects, not just one.

    Before, I had a serialized field on the player object:
    [SerializeField] ParticleSystem deathParticles;

    I'd simply just drag one particle effect to that field in the Inspector.

    I could trigger easily trigger it on an OnCollisionEnter event.
    deathParticles.Play();

    But it only accepts one particle effect. Is there any easier way to get all the explosion particle effects to trigger, rather than having to create 10 or so SerializedFields?

    My first thought would be to create tags for the particles effects, and tag them all as "explosion" and somehow trigger only the particle effects whose tags were labeled as "explosion", but is this the best way, and how do I find them all in code?
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    You could author your particle system to use sub-emitters, rather than separate top-level emitters.

    Or, you could get all PartcileSystem components in the hierarchy and tell them each to play
    Code (csharp):
    1. var allParticles = deathParticles.GetComponentsInChildren<ParticleSystem>();
    2. foreach (var ps in allParticles)
    3. {
    4.     ps.Play();
    5. }
     
    richardkettlewell likes this.
  3. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,240
    If you make the other systems child gameobjects of one master effect, Play on the master (parent) effect takes a bool argument that will also play all the child gameobjects.
     
  4. jeremy_mccloud

    jeremy_mccloud

    Joined:
    Feb 4, 2018
    Posts:
    8
    That did the trick! Thank you! I didn't realize it was so easy! LOL.
     
    richardkettlewell likes this.