- Home /
Particle memory overhead
So im using this script to move all particles to a spot. But when i have many particles(500 - 1000) i have massive memory overhead.Without this script it dont lags. How can i move particles to the spot without memory overhead?Thanks!
void Update()
{
// Extract copy
Particle[] particles = particleEmitter.particles;
// Do changes
for (int i = 0; i < particles.Length; i++)
{
particles[i].position = Vector3.MoveTowards(particleEmitter.particles[i].position, spot.transform.position, Time.deltaTime * speed);
}
// Reassign back to emitter
particleEmitter.particles = particles;
}
Answer by CHPedersen · Aug 06, 2013 at 09:52 AM
This looks familiar. :)
Some of the memory overhead I mentioned in your other post about particles has been mitigated by not accessing the emitter's particles property on every iteration of the for-loop's declaration, but now that I have a closer look, I see it's actually still going on inside the MoveTowards method. You're supposed to be using the temporary copy stored in the particles variable instead of accessing the emitter's array, so it should be this, actually:
void Update()
{
// Extract copy
Particle[] particles = particleEmitter.particles;
// Do changes
for (int i = 0; i < particles.Length; i++)
{
particles[i].position = Vector3.MoveTowards(particles[i].position, spot.transform.position, Time.deltaTime * speed);
}
// Reassign back to emitter
particleEmitter.particles = particles;
}
This keeps the emitter's array entirely out of the for-loop. Sorry about that slip-up. But even though we have now removed it entirely from the for-loop, you still incur the overhead to a lesser extent because you're accessing it in the Update method, so it happens once every frame. This memory overhead is in addition to the CPU cost of looping over a potentially large collection, also every frame. All in all, this is still a bad idea. :)
Since your goal seems to be to control the direction the particles move in, why not just set the particles' velocity? See this page on how to configure the shuriken particle system:
http://docs.unity3d.com/Documentation/Manual/ParticleSystemModules40.html
Moving particles towards a fixed destination is controlled internally in the particle system. It should happen on a frame by frame basis using the velocity of the particles, so you shouldn't have to move them by script in the first place.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Turn weather on and off 2 Answers
Distribute terrain in zones 3 Answers
Can you still not change the Velocity of spawned particles? 2 Answers