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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

destroy single particles?

Discussion in 'General Graphics' started by larswik, Aug 26, 2017.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I found the same question asked in 04' 13 years ago with no answer. I have a bomb graphic that drops from the air. After 3 seconds it explodes ( destroy(); ) and a particle effect is created with 10 particles that have have a material attached that is shrapnal png image. This then rains down holy hell on the town below. When a particle collides with an object the delegate method is called and I want to destroy this 1 single particle.

    Code (CSharp):
    1. void OnParticleCollision(GameObject other){
    2.         print("Particle hit!");
    3.         Destroy(other,0f);
    4.     }
    But the destroy is destroying the entire particle system, not just the one particle. Can you destroy just one since particle on collision?
     
    HyronXIII and agehm like this.
  2. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    You don't need script for this. Enable "Collision" module in particle system and set "life loss" to 1, change the layers in "collide with" and your object to destroy the particle so the particle only collides with collider of specific layer.
     
    Oelson, lacas8282, linenum and 26 others like this.
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I spent my whole Saturday, 8 hours so far researching this and 30 seconds after reading your answer I see the individual particles being deleted as they hit the gameObject. Thank You!
     
    Siliko likes this.
  4. blekkh

    blekkh

    Joined:
    Feb 14, 2019
    Posts:
    19
    Is it possible to change the value of the particlelifetimeloss per layer, for example: on the player the particle like "destroys" immediately, but on the ground they stay a little longer?
     
  5. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,166
    No, sorry, we don't have anything built-in for this.
     
  6. blekkh

    blekkh

    Joined:
    Feb 14, 2019
    Posts:
    19
    thank you for replying tho
     
    richardkettlewell likes this.
  7. Rafaelski

    Rafaelski

    Joined:
    May 1, 2019
    Posts:
    11
    Hello, well, I think you could use Trigger together with the Collider. So, you enable the Collision normally, but when it hits the Player, you use the Trigger module (built-in with the particle system) and tell them to kill this particle.
     
    Siliko likes this.
  8. test3332y

    test3332y

    Joined:
    Nov 4, 2020
    Posts:
    1
    Is there a way of making a particle damage the player and then destroying that single Particle by using the Triggers Component and a script? And how do I even destroy a single Particle from a script?
     
  9. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,166
    If you set the particle age in the OnParticleTrigger callback you can kill the particle.
     
    Alic likes this.
  10. H_Ishfaq

    H_Ishfaq

    Joined:
    Jun 25, 2019
    Posts:
    4
    @ifurkend please can you tell that , is it optimized for mobile games . like modern combat 5 game .
     
  11. simonvutov1

    simonvutov1

    Joined:
    May 28, 2020
    Posts:
    4
    The same thing happened to me, I spent 3 hours trying to figure it out and then i read this! :) thanks
     
  12. zorigdavaa

    zorigdavaa

    Joined:
    Feb 4, 2021
    Posts:
    1
    How to destroy one particle (such as fire) with collision or trigger of other particle (such as water).
     
  13. Rushidan

    Rushidan

    Joined:
    May 26, 2019
    Posts:
    1
    You can destroy all particles like this:
    Code (CSharp):
    1. public void DestroyAllParticles(ParticleSystem ps, bool stop = true)
    2.     {
    3.         ParticleSystem.Particle[] particles = new ParticleSystem.Particle[ps.particleCount];
    4.         ps.GetParticles(particles);
    5.         int count = ps.particleCount;
    6.         for (int i = 0; i < count; i++)
    7.         {
    8.             particles[i].remainingLifetime = 0f;
    9.         }
    10.         ps.SetParticles(particles);
    11.  
    12.         if (stop) ps.Stop();
    13.     }
     
    pistoleta likes this.