- Home /
Adjusting speed of existing particles
I have a particle system and I want to control the velocity of emitted particles. I can modify the emission speed of new particles, but I want to also adjust the velocity of particles that already exist. I assumed I could do that by getting the particles, updating the velocities and setting the particles again, but when I then check the velocities the next frame, they're unchanged.
Here's the code:
void LateUpdate()
{
if (mUpdateParticleSpeed)
{
mUpdateParticleSpeed = false;
// Update particle velocities, assumes constant velocity
int numParticles = particleSystem.particleCount;
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[numParticles];
particleSystem.GetParticles(particles);
for (int i = 0; i < particles.Length; i++)
{
var particle = particles[i];
particle.velocity = particle.velocity.normalized * mParticleSpeed;
}
particleSystem.SetParticles(particles, numParticles);
}
}
Any suggestions as to the problem? I'll post an update if I figure it out myself.
Thanks.
Answer by Lttldude · Apr 15, 2012 at 09:02 PM
Your code is all good, except you didn't save the changed particle back to the array so it could be set back to the ParticleSystem.
var particle = particles[i];
particle.velocity = particle.velocity.normalized * mParticleSpeed;
particles[i] = particle;
OR
particle[i].velocity = particle[i].velocity.normalized * mParticleSpeed;
Also, you are probably already aware of the mUpdateParticleSpeed variable, but as the code is written now, this will only affect particles that have already been emitted. If they are starting to emit on play, then none will be affected because it will only run for one frame when no particles are emitted. I emitted that whole conditional statement to make sure the code worked.
Josh
Ah, of course - the particle is a struct not a class!
As for new particles, I have code to adjust the speed, I just didn't post it for clarity.
Thanks for spotting my mistake!
Your answer
Follow this Question
Related Questions
Setting the position of particles in a (shuriken) ParticleSystem 3 Answers
Scale Shuriken Particle System with Parent Transform 5 Answers
Particles emmiting all over the shape, is it possible make them spawn gradually from a set point? 0 Answers
PS Custom Vertex Streams to Particle Trail 0 Answers
How can I create a particle 'vortex' or implosion using Shuriken? 2 Answers