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.
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.
public class ParticleCollider : MonoBehaviour
{
private void OnParticleCollision(GameObject other)
{
List<ParticleCollisionEvent> events;
events = new List<ParticleCollisionEvent>();
ParticleSystem m_System = other.GetComponent<ParticleSystem>();
ParticleSystem.Particle[] m_Particles;
m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles];
ParticlePhysicsExtensions.GetCollisionEvents(other.GetComponent<ParticleSystem>(), gameObject, events);
foreach(ParticleCollisionEvent coll in events)
{
if(coll.intersection!=Vector3.zero)
{
int numParticlesAlive = m_System.GetParticles(m_Particles);
// Check only the particles that are alive
for (int i = 0; i < numParticlesAlive; i++)
{
//If the collision was close enough to the particle position, destroy it
if (Vector3.Magnitude(m_Particles[i].position - coll.intersection) < 0.05f)
{
m_Particles[i].remainingLifetime = -1; //Kills the particle
m_System.SetParticles(m_Particles); // Update particle system
break;
}
}
}
}
}
}
Use it on the object that has the collider.
Remember to check Send Collision Messages on Collision module of the particle system object.
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?
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.
too late but I solved that problem by setting: particle sys > sollision => Min kill speed = 1000 (this is infinite number in my case), Max kill speed = 0 (this is min speed in my case)