Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Particle System] Looking for way to spawn subemitter type depending on how particle died.

Discussion in 'General Graphics' started by Sharlatan, Jun 9, 2018.

  1. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Hello everybody!

    I've got the following situation:

    - I have a 2D game where you control a ship
    - Cannonballs are generated via particle system. Every time the player fires, I just spawn a bunch of particles
    - There are two ways in which cannonballs "die": Either they hit another ship or they fly long enough that they crash into the water (although I don't really have the concept of "water" per se, since there's no need for it. Basically "crashing into the water" just means "particle reaches end of life time set in particle system").
    - Depending on how a particle dies, I'd like to spawn another particle effect, an explosion when the particle hits a ship, a splash of water if it reaches it's end of life without hitting a ship.

    Now, I'm a bit stuck on how to achieve this.

    For the explosion I see two possibilities:
    - Either I handle creation of the emitter in OnParticleTrigger()/OnParticleCollision().
    - Or I add a subemitter and set it to Trigger/Collision.

    But now I'm stuck on the "splash" effect particle emitter.
    I first thought about adding a sub emitter and tying it to "Death" but the problem is, of course, that Death will also be triggered when a particle collides with a ship. This leads to a situation, where the splash effect is cast correctly whenever a particle reaches the end of it's natural life time but unfortunately it also gets spawned every time a particle gets destroyed because it hits a ship.

    Any ideas as to how to solve this problem would be highly appreciated!

    Thanks and have a nice weekend.
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    Tricky!

    It’s a bit hacky, but you could allow both to be spawned in the collision case, then use Get/SetParticles on the water effect, and remove any particles that have a small lifetime.

    I can’t think of any other way to do this..
     
    karl_jones likes this.
  3. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Dear Richard

    Thanks a lot for your help!

    Did I understand you correctly? You would use e.g. the OnParticleCollision() event to check when a ship was hit and use that event to trigger some code that will grab all the particles from the water effect emitter, check, which ones have only been alive for a very short time ( => must have spawned just recently) and delete them?

    Sounds like a hack I could live with! The only thing I'm wondering is, if this wouldn't lead to a situation where I get kind of visual "glitches" where I see the unwanted water effect particles for a frame or two before they get removed. Sorry, I'm not that familiar with the system and in what order particles get created/rendered etc and how it relates to the Monobehavior callback functions and such in terms of execution order. Do you think/know if there'd be a surefire way to detect and remove those particles before they get rendered for the first time?

    Thanks again and have a nice day!
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    Yep you understood me correctly.

    Yes maybe they would show up for a frame or 2, I’m not sure. Maybe the size/color over life curve of the water fx could be set up so you can kill them before they are truly visible.

    You may also find OnParticleCollision is too soon for this hack.. so you could either set a flag to do it on the next Update, or maybe a “yield return null” would work.