- Home /
Particle System, change the velocity by script
How could I change the "Velocity over Lifetime" setting of a Partycle System using a C# script?
Thank you guys!
Answer by GerryM · Nov 24, 2012 at 09:11 AM
You need to set the particles, too. In case you want to change the velocity according to lifetime, try something like this:
void LateUpdate () {
ParticleSystem.Particle[] p = new ParticleSystem.Particle[particleSystem.particleCount+1];
int l = particleSystem.GetParticles(p);
int i = 0;
while (i < l) {
p[i].velocity = new Vector3(0, p[i].lifetime / p[i].startLifetime * 10F, 0);
i++;
}
particleSystem.SetParticles(p, l);
}
well... I still cannot understand exactly how does it work...
For example in my starting situation, velocity over lifetime is X=50, Y, 0, Z= 0, and the particles move along the X axis.
When a particular event occur I'd like that the particles move along the Y axis, and this can be done by change the Inspector property "Velocity over lifetime" at X = 0, Y= 50, Z= 0.
I'd like to control this behavior with a C# script.
It would be enough to change the "Veloity over lifetime" property, but it seems this is not possible.
Your script change the velocity of the particles but in a strange way...
So for example if I write:
p[i].velocity = new Vector3 (0, 50, 0)
the particles don't move along the Y axis...
Thanks for your help...
It's tricky, you need to change (in the inspector) from local to world space to have y up. Or you do rotate the particle system.
hmmm, I've realised what was the problem:
I've set in the inspector a velocity over lifetime of (50, 0, 0) and the particles move along the X axis.
If in a script I add the code:
p[i].velocity = new Vector3(0, 50, 0)
the particles won't move vertically along the Y axis, because they already have a X=50... So they will move at 45° degrees.
hmmm, so now I'm not sure if this is the best way to ROTATE my particles along its origin...
oh, in order to rotate a particle system, it's enough to turn the object the particle is attached with... like an empty object for example....