- Home /
Can you still not change the Velocity of spawned particles?
Hi, I found this Question from 2015 Can you change the speed of a spawned particle? Is this stll true? And what would be a way around that because i really need to change the speed of the whole particle systhem from script !
Answer by Nighfox · Feb 02, 2018 at 03:06 PM
Changing values of newly created particles:
Get ParticleSystem.MainModule
from ParticleSystem
to access those values:
public ParticleSystem ps;
ps = GetComponent<ParticleSystem>();
ParticleSystem.MainModule main = ps.main;
main.startSpeed = 6;
Note that this is only available starting from Unity 5.5.
Docs: https://docs.unity3d.com/ScriptReference/ParticleSystem.MainModule.html
Changing values of existing particles:
Get the particles first by ParticleSystem.GetParticles
, modify those values, then set it back again by ParticleSystem.SetParticles
:
ParticleSystem ps;
int particleCount = ps.particleCount;
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleCount];
ps.GetParticles(particles);
for (int i = 0; i < particles.Length; i++)
{
particles[i].velocity *= 2f;
}
ps.SetParticles(particles, particleCount);
Docs: https://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html https://docs.unity3d.com/ScriptReference/ParticleSystem.SetParticles.html
doesn't that only change the speed for newly spawned particles?
Yes, so if you want to change values of existing spawned particles, just get the particles first by ParticleSystem.GetParticles
, modify those values, then set it back again by ParticleSystem.SetParticles
:
ParticleSystem ps;
int particleCount = ps.particleCount;
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleCount];
ps.GetParticles(particles);
for (int i = 0; i < particles.Length; i++)
{
particles[i].velocity *= 2f;
}
ps.SetParticles(particles, particleCount);
Answer by Tom507 · Feb 12, 2018 at 04:01 PM
So your answer was great @NightFox , but i ended up using the Simulation Speed from main, because it effectively changes both the Velocity and the startSpeed of all particles while maintaining the Lenght and overall shape of the PS, using less precios cpu Time, and being easyer to implement.
The only downside is that stuff like the trail lenght of particles stayes the same, But for that detail I don't want to go down the rabbithole of changing every single parameter to keep the look of the PS the same.
SO my function ended up being as simple as this :
public void ParticleSpeed (ParticleSystem ps, float defaultVelocity, float VelocityMultiplyer){
ParticleSystem.MainModule main = ps.main;
main.simulationSpeed = defaultVelocity * VelocityMultiplyer;
Debug.Log ("Particle simSpeed : " + main.simulationSpeed);
}
but thanks again for your Answer.
Your answer
Follow this Question
Related Questions
How to increase and decrease the animation speed? 1 Answer
Bullet not moving from script 3 Answers
Maintaining speed after teleportation [c#] 5 Answers
Ball doesn't increase speed. C# 1 Answer