Hello!
I’m trying to make a particle emitter whose particles go from point A to point B with a fixed speed.
To achieve this I’m calculating their lifetime based on the distance to the target and the speed they’ll have.
The fist thing I tried was this:
_particleSystem.startLifetime = distanceToTarget / particleSpeed;
but is deprecated so I’d rather avoid it. I found I’m supposed to use this instead:
ParticleSystem.MinMaxCurve curve = new ParticleSystem.MinMaxCurve(distanceToTarget / particleSpeed);
_particleSystem.main.startLifetime = curve;
but I get an error saying
Cannot modify a value type return value of `UnityEngine.ParticleSystem.main’. Consider storing the value in a temporary variable
Which makes sense because the main module is read only but leaves me stuck with using deprecated code.
Is there any workaround for this?
Thanks in advance!
ParticleSystem.MainModule psmain = _particleSystem.main;
psmain.startLifetime = distanceToTarget / particleSpeed;
ref: Particle System Modules - FAQ | Unity Blog
Just as a follow up to this, At first I couldn’t get this to work properly in 2019 for a water splash effect I was working on in my game where I wanted the size of the splash to be dependant on the speed the character entered the water.
Originally I was doing it this way:
The particle emitter was set to not loop and play on awake
I instantiated the particle emitter when the player entered the water
Set up the variables
and it didn’t work properly
Instead I found this worked:
The particle emitter was set to not loop and not start on awake
I instantiate the particle emitter
Set the variables
Call the Play() function on the emitter after setting the variables
That seems to work
Maybe this seems obvious to people but it wasn’t obvious to me and I wasted at least an hour finding a fix.