- Home /
How to fade out decal particles using color over lifetime?
I'm using a particle system to display decals. Play on awake and loop are disabled so the system never plays. Emission is disabled. Color over lifetime is a gradient going from white with alpha 255 to white with alpha 0.
To write the particles, I set their position, 3drotation, startSize and startColor. They appear as they should, no problem so far.
Now at some point I want to fade out the currently displaying decals. For that I get all the particles, loop through them and set their startLifetime to a few seconds. Then I write them back with SetParticles.
Problem is, they don't fade out but instead all the particles are killed immediately. I checked by trying to get the system's particles in the next Update call (which is only ~1 frame later, not anyone near the lifetime I set) but by then the system is already empty.
Answer by KayH · Nov 23, 2017 at 04:29 PM
To sum up the solution I found with the help of ifurkend:
Both startLifetime and remainingLifetime need to be set (to the same value).
The particle system needs to be set to play so it actually animates. I set it to stop again when I replace the old decals with new ones, because those should stay put.
Answer by ifurkend · Nov 23, 2017 at 09:40 AM
Just a thought without testing: First you can set startlifetime in the editor to "infinity" if it's indefinite, also just disable color over lifetime module. In the script where you loop the all got particles, don't change their lifetime, but do a lerp of the alpha of startColor (Color32), sth like:
m_particles[i].startColor = new Color32 (m_particles[i].startColor.r, m_particles[i].startColor.g, m_particles[i].startColor.b, Mathf.Lerp(m_particles[i].startColor.a, 0f, time.deltaTime/fadeoutDuration);
if(m_particles[i].startColor.a == 0f){
m_particles[i].remainingLifetime = 0f;
}
Preferably do it in a Coroutine instead of Update.
Thanks for the suggestion and yes, I could always do it myself like that. But preferably I'd like to use the functionality offered by the particlesystem class.
So what I'm looking for is an explanation for how to do this with color over lifetime, or an explanation why it simply can't work with lifetime. I'm puzzled why setting a startingLifetime kills the particles immediately (it also does when I do it when they're first created). I don't think this is supposed to happen.
Yes, I have. Actually that was my first approach. But setting remainingLifetime had no effect at all, i.e. the particles neither faded nor did they disappear/get killed.
Upon closer inspection, the array of particles, which clearly has the remainingLifetime set to 2 for all particles, gets updated when I pass it to SetParticles. Right on the next execution step all the particles suddenly have their remainingLifetime set to 0. Nothing happens afterwards.
I also tried setting both startLifetime and remainingLifetime at the same time, but that results in all of them getting killed immediately also.
So to summarize, setting remainingLifetime does nothing at all and its value gets reset to 0 immediately. And setting startLifetime results in the particles getting instantly killed.
Your answer
Follow this Question
Related Questions
How to destroy / hide a single particle? 2 Answers
Assign random start lifetime for particle system from script 2 Answers
Is it possible to control a particles lifetime(duration) by distance rather than time using code 1 Answer
Dynamic Particle System Lifetime?? 2 Answers
(STILL STUCK!) Align Particle Collsion Sub-Emitter (or decal?) with Surface Normal 1 Answer