Hey, guys. I got into a side project recently where I’m trying to build out a script that gives me more control over particle movement. This has ended up being a pretty big headache, and one of the biggest hurdles I’ve run into is I can’t for the life of me figure out how the particle system arranges particles in it’s array. For example, let’s say you have 5 active particles in your particle array. If you terminate the particle which resides at index 2 of the array, how is the array adjusted next time you reference it? Does it simply eliminate that position as it would in a list and move everything above it down an index, or is the order in which it pulls the active particles totally random?
I know I’m not doing a great job of explaining what I’m looking for, but not sure how to make it any clearer. Hopefully the code bellow will shed some light. Line 17 is where the list of active particles is generated, and line 30 is where particles that have reached the end of their time-alive are removed. When I remove a particle at line 30, line 17 seems to completely re-mix the order of the particles in the list. I can’t figure out how to keep track of which particle ends up at which index. Help appreciated.
private void FixedUpdate()
{
AddParticle();
RemoveOld();
}
void AddParticle()
{
clock += Time.deltaTime;
if (clock < 1 / fireRate && !isEnabled) { return; }
clock = 0;
//Add a particle to the system
ps.Emit(1);
// assign particles to particles
ps.GetParticles(particles); //This is where the particle list is generated as particles
numParticles++;
}
void RemoveOld()
{
for (int i = 0; i < numParticles; i++)
{
parts[i].clock += Time.deltaTime;
if (parts[i].clock > timeAlive)
{
//I the parts index to always match the same particle index
parts.RemoveAt(i);
particles[i].remainingLifetime = -1; //killing particle
numParticles--;
}
}
ps.SetParticles(particles, numParticles); //This must be included to apply changes to particles
}