I found a lot of topics saying that if I would like to remove a specific particle, I simply have to set it's LifeTime
to -1
.
I know my loop is working correctly, as the movement of each particle goes as planned AND I can see the "remove particle" Debug line in my log the moment it reaches it's destination. Did anything change over time, or am I missing something simple?
I'm using Unity 5,4,3f1 Personal
void Update ()
{
if(Input.GetKeyDown(KeyCode.Space)) PlayParticleEffect();
if (particleSystem != null) {
particles = new ParticleSystem.Particle[particleSystem.particleCount];
int count = particleSystem.GetParticles (particles);
for (int i = 0; i < count; i++) {
ParticleSystem.Particle particle = particles [i];
float dist = Vector3.Distance (particleTarget.transform.position, particle.position);
if (dist > 0.1f) {
particle.position = Vector3.MoveTowards (particle.position, particleTarget.transform.position, Time.deltaTime * 10);
particles [i] = particle;
} else {
particle.lifetime = -0;
Debug.Log ("remove particle");
}
}
particleSystem.SetParticles (particles, count);
}
}
You just need to set the remaining lifetime of the particle to 0 (if it's set to 0, the particle will disappear).
Your code doesn't work because you forgot to add particles [i] = particle;
in the else branch of your if, you're never setting the lifetime to 0 to the actual particle:
if (dist > 0.1f) {
particle.position = Vector3.MoveTowards (particle.position, Vector3.zero, Time.deltaTime * 10);
particles [i] = particle;
} else {
particle.remainingLifetime = 0;
particles [i] = particle;
}
P.S.: I used remainingLifetime
instead of lifetime
since I'm on Unity 5.5