Keeping track of particles and changing their size

I’m writing a system that has several particle emitters respond to audio input and change size. The basic issue is:

  • I have several particle systems emitting
  • I need to go through each live particle in each system and change their size.
  • Particles change their size based on a sizeModifier float (say 1.0 to 1.2). One tick it may be 1.2, another it could be 1. (Imagine the volume of the music causing the particles to pulse in size between a min and max value).
  • Many of the particles will spawn with their size between two starting sizes, as specified in the inspector, so I can’t just access the ParticleSystem and take it’s startSize value.

My initial thought was along these lines:

for (int i = 0; i<transform.childCount; i++)
{
ParticleSystem p = transform.GetChild(i).GetComponent<ParticleSystem>();
if (p.gameObject.activeSelf)
{
ParticleSystem.Particle[ ] activeParts = new ParticleSystem.Particle[p.main.maxParticles];
int numParticlesAlive = p.GetParticles(activeParts); 
for (int j = 0; j<numParticlesAlive; j++)
{
float curSize = activeParts[j].startSize;//(p);
float newScale = curSize * scaleMulti;
activeParts[j].startSize = newScale;
}
p.SetParticles(activeParts, numParticlesAlive);
}
}

The obvious problem with that is that every tick they grow exponentially as their startSize is constantly being multiplied and then saved back on the particle, instead of having it’s startSize be it’s initial size times the multiplier.

I don’t need any help writing the specifics of the code, just a point in the right direction. Could I keep track of every particle in a dictionary let’s say, keeping track of startSize at spawn, making sure to clear off inactive/destroyed particles. Then when I loop through the particles, I use their initial startSize rather than the current one which keeps getting changed? If that’s possible, how do I keep track of new particles emitted and old ones being destroyed/made inactive?

Thanks in advance.

There’s a little defect in unity what likely will not be fixed. If you set a meshrenderer as particle emitter, the scale of renderer will affect the scale of each particle. Probably you may utilize this if it affects living partices too

Also that new operator hurts your code performance, it’s better to use preallocated array for this. There’s a max particle count in particle system, use it for sizing array correctly.

@palex-nx Thanks for your response.

When I change the scale of the shape when it is a Mesh Renderer, it doesn’t increase the size of the particles. When I change the size of the object that the ParticleSystem is attached to, regardless of if it has a MeshRenderer particle emitter, it scales the particles but also the distance between them. So I end up getting more of a zoom effect rather than individual particles increasing their size. I’m also aware that there’s a scaling mode on the main of the Particle System. This gives me the same effect as described above, the particles are indeed getting bigger, but also further apart.

Is anyone aware of what parameters of the Particle System get scaled with the parent object? For example, do I need to increase the size of the transform by 20% but decrease several other things by 20% so that the particles appear to be getting bigger without other parameters changing?

May be I just explained my idea not good enought, check this topic Individual particles get scaled when using Mesh Renderer shape?