Setting the velocity (direction) of all particles in a ParticleSystem

I’m making a space exploration game in which the player will encounter rings around planets. In my game I keep the player (ie camera) at position 0, 0, 0 and all other objects move around the player as the player ‘moves’.

I would like to simulate small particles when the player gets very near a set of planetary rings. To do this I have set up a particle system emitting from the perimeter of a sphere also positioned at 0, 0, 0 (around the player).
The problem is because the player isn’t actually ‘moving’ I’d like to set the direction of all emitted particles to the inverse of the player’s velocity (the movement that all world objects make each frame).

How do I set the velocity of all the particles in a particle system? I have seen these pages…

…but I don’t see something there to set the velocity of an existing in-editor ParticleSystem instance. The EmitParams solution would need to involve creating a new EmitParams every frame and that doesn’t sound right to me.
Any pointers, much appreciated, thanks.

Try this: Unity - Scripting API: VelocityOverLifetimeModule

There are examples in each property page, eg Unity - Scripting API: ParticleSystem.VelocityOverLifetimeModule.x

1 Like

OK, thanks. I’ll give that a look.
Just wondering: Performance-wise would it be better to use VelocityOverLifetimeModule or to have the ParticleSystem use external forces and direct the particles with a WindZone?
Cheers.

The Velocity Over Lifetime module will be more efficient, but it would probably be close.

The External Forces module has to check that the wind zone affects the particle system, and the maths is a bit more complicated in that module, too. But not by much, if using a directional force.

Also consider that External Forces applies forces to the particles, and the Velocity module directly sets their velocity. They are not going to have the same result. Think about the basic physics equations i.e. Force = mass * acceleration, and velocity is changed by the force applied over time. Whereas the Velocity module simply sets the velocity directly and ignores the equations :slight_smile:

Thanks for the pointers. I’ve been looking at how to implement it, but I’m pretty sure I don’t fully understand it.
I think I’ve figured out that the key parts are the xMultiplier, yMultiplier and zMultiplier properties. The ‘curves’ (even though they’ll be set to 1 for their durations) can all be set up at Start and multiplied at Update.
My version of Unity is throwing an error at the xMultiplier line unfortunately, saying that’s not defined in ParticleSystem.VelocityOverLifetimeModule. But my machine here has Unity 5.4 so I’m guessing (hoping) that it will work on my machine at home that has 2018.3
If any of this sounds wrong, please feel free to let me know.
Thanks again.

Yikes, 5.4 is old :wink:

If you do need it to be 5.4 compatible, there is a much simpler script interface available in 5.4: https://docs.unity3d.com/540/Documentation/ScriptReference/ParticleSystem.VelocityOverLifetimeModule.html

To get simple constant values, with no curve behavior, just set the properties to constants via

var module = ps.velocityOverLifetime;
module.x = new ParticleSystem.MinMaxCurve(1.0f);

etc.
You might even be able to just say

var module = ps.velocityOverLifetime;
module.x = 1.0f;

but I’m not sure if we added that shorthand in a more recent version.

1 Like