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.