Question by
jigwiz · Jul 04, 2017 at 02:21 AM ·
instantiate3dparticlesshooter
How do I destroy a single particle when it collides with a certain object
I have spheres that are instantiated when I press fire. These spheres are being shot at a particle system. I want the individual particle that is struck by the sphere to be destroyed when the sphere touches it. Selecting trigger doesn't work in particle system module, because though the particles are destroyed by the sphere placed in the scene they are not destroyed when the instantiated sphere touches them.
Here is the GetParticles script I'm using:
public class GetParticles : MonoBehaviour {
ParticleSystem m_System;
ParticleSystem.Particle[] m_Particles;
public float m_Drift = 0.01f;
private void LateUpdate()
{
InitializeIfNeeded();
// GetParticles is allocation free because we reuse the m_Particles buffer between updates
int numParticlesAlive = m_System.GetParticles(m_Particles);
// Change only the particles that are alive
for (int i = 0; i < numParticlesAlive; i++)
{
m_Particles[i].velocity += Vector3.up * m_Drift;
}
// Apply the particle changes to the particle system
m_System.SetParticles(m_Particles, numParticlesAlive);
}
void InitializeIfNeeded()
{
if (m_System == null)
m_System = GetComponent<ParticleSystem>();
if (m_Particles == null || m_Particles.Length < m_System.main.maxParticles)
m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles];
}
}
Here is the Particle Trigger script I'm using:
public class TriggerScript : MonoBehaviour
{
public ParticleSystem ps;
public List<ParticleSystem.Particle> enter = new List<ParticleSystem.Particle> ();
public List<ParticleSystem.Particle> exit = new List<ParticleSystem.Particle> ();
void OnEnable()
{
ps = GetComponent<ParticleSystem>();
}
void OnParticleTrigger()
{
int enterParticleNumber = ps.GetTriggerParticles (ParticleSystemTriggerEventType.Enter, enter);
int exitParticleNumber = ps.GetTriggerParticles (ParticleSystemTriggerEventType.Exit, exit);
// iterate
for (int i = 0; i < enterParticleNumber; i++)
{
ParticleSystem.Particle p = enter[i];
//put event here
}
for (int i = 0; i < exitParticleNumber; i++)
{
ParticleSystem.Particle p = exit[i];
p.startColor = new Color32(0, 255, 0, 255);
exit[i] = p;
}
// set
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);
}
}
Comment