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

Remove a particle on particle collision

Discussion in 'Scripting' started by SamOatesGames, Nov 22, 2016.

  1. SamOatesGames

    SamOatesGames

    Joined:
    May 16, 2013
    Posts:
    6
    Hello,

    I have a case where by I want to draw on a texture when a particle hits a given collider.
    I have this working, however the particle bounces on the collider then hits again at another point.
    What I want to do is delete the particle when it hits a given collider.

    I can get the particle collision events, but this does not give me the particle object so I can't remove it from the particle systems particles array.

    Does anyone have any ideas on how I could achieve this.

    Thanks,
    Sam.
     
  2. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
  3. SamOatesGames

    SamOatesGames

    Joined:
    May 16, 2013
    Posts:
    6
    But how do I know which particle it is?
    If the particle collides with a floor for example, I still want it to bounce. If the particle collides with a certain object, I want the particle to be destroyed.

    Sam.
     
  4. luuchowl

    luuchowl

    Joined:
    May 17, 2016
    Posts:
    2
    Hello, I'm kinda late, but here it is:

    Code (CSharp):
    1. public class ParticleCollider : MonoBehaviour
    2. {
    3.     private void OnParticleCollision(GameObject other)
    4.     {
    5.         List<ParticleCollisionEvent> events;
    6.         events = new List<ParticleCollisionEvent>();
    7.  
    8.         ParticleSystem m_System = other.GetComponent<ParticleSystem>();
    9.  
    10.         ParticleSystem.Particle[] m_Particles;
    11.         m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles];
    12.  
    13.         ParticlePhysicsExtensions.GetCollisionEvents(other.GetComponent<ParticleSystem>(), gameObject, events);
    14.         foreach(ParticleCollisionEvent coll in events)
    15.         {
    16.             if(coll.intersection!=Vector3.zero)
    17.             {
    18.                 int numParticlesAlive = m_System.GetParticles(m_Particles);
    19.  
    20.                 // Check only the particles that are alive
    21.                 for (int i = 0; i < numParticlesAlive; i++)
    22.                 {
    23.                  
    24.                     //If the collision was close enough to the particle position, destroy it
    25.                     if (Vector3.Magnitude(m_Particles[i].position - coll.intersection) < 0.05f)
    26.                     {
    27.                         m_Particles[i].remainingLifetime = -1; //Kills the particle
    28.                         m_System.SetParticles(m_Particles); // Update particle system
    29.                         break;
    30.                     }
    31.                 }
    32.             }
    33.         }      
    34.     }
    35. }
    Use it on the object that has the collider.
    Remember to check Send Collision Messages on Collision module of the particle system object.
     
    Zhelatin likes this.
  5. Artem-Zhyganov

    Artem-Zhyganov

    Joined:
    Aug 13, 2013
    Posts:
    2
    thank you luuchowl for sharing the code!
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Why not just use the built-in "Min Kill Speed" and "Max Kill Speed" in the Collision section of the particle system to kill the particles on Collision?
     
  7. contact_unity364

    contact_unity364

    Joined:
    Sep 28, 2020
    Posts:
    5
    In the previous example by luuchowl you would need to not create a new instance of the particles array (m_Particles) each collision. You want to have a variable in your instance to hold that data to avoid constant memory reallocation.

    To solve this, I used the collision "lifetime loss" property which by default is set to 0. But you can set it to 1 for the particles lifetime to be maxed out on the collision so it disappears. Or you set to 0.1 for it to lost 10% on the collision, which may be more interesting. But this is absolutely the way to make a particle disappear on collision.