- Home /
Lerp an Alpha Gradient in Particle System Color over lifetime In script
I'm using a snow particle effect, on a 2d map and transition to rain and others depending on where your at on the map. problem I'm facing is the snow stays too long and looks sloppy when in areas it shouldn't be. I cant just set the color over lifetime alpha because the lifetime itself is too long. It needs to be so the particles don't disappear in the middle of the screen too early. I've tried changing the alpha in script but the sudden change in alpha does not look good. I've been playing with gradient keys, canvas groups and lerping methods to no avail. does anyone have any idea if you can lerp from one gradient alpha to another, I've found plenty of resources on color.lerp but not for the alpha. oddly enough I got mathf.pingpong to give me the effect i wanted but it would constantly swap back and forth between alpha 0 and 255 which I assume is correct hence "pingpong" although when replaced with the lerp method it didn't do anything except change alpha from 255 to 250..not sure why..
Heres what I have, this is called in the start method I need to add the alpha change in the //Show snow section
private IEnumerator ShowEnvironmentParticleCo()
{
//Rain Particle System
ParticleSystem rainParticle = rain.GetComponent<ParticleSystem> ();
var rainEm = rainParticle.emission;
//Mist Particle System
ParticleSystem mistParticle = mist.GetComponent<ParticleSystem> ();
var mistEm = mistParticle.emission;
var mainMist = mistParticle.main;
var mistColor = mistParticle.colorOverLifetime;
Gradient ourGradient;
ourGradient = new Gradient ();
//Snow Particle System
ParticleSystem snowParticle = snow.GetComponent<ParticleSystem> ();
var snowEm = snowParticle.emission;
//Snow Particle System 2
ParticleSystem snowParticle2 = snow2.GetComponent<ParticleSystem> ();
var snowEm2 = snowParticle2.emission;
var snowColor2 = snowParticle2.colorOverLifetime;
//Flash Particle System
ParticleSystem flashParticle = flash.GetComponent<ParticleSystem> ();
var flashEm = flashParticle.emission;
//Show Rain based on scroll rect position
if (scrollBar.value > 0.13 && scrollBar.value < 0.3)
{
rainEm.enabled = true;
GameObject.Find ("RainPrefab2D").GetComponent<AudioSource> ().enabled = true;
}
else
{
rainEm.enabled = false;
GameObject.Find ("RainPrefab2D").GetComponent<AudioSource> ().enabled = false;
}
//Show Mist based on scroll rect position
if (scrollBar.value > 0.59 && scrollBar.value < 0.86)
{
mistEm.enabled = true;
mainMist.simulationSpeed = 0.25f;
//Change Mist Alpha
ourGradient.SetKeys (
new GradientColorKey[] { new GradientColorKey (Color.white, 1.0f), new GradientColorKey (Color.white, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey (0.3f, 0.0f), new GradientAlphaKey (0.0f, 1.0f) });
mistColor.color = ourGradient;
}
else if (scrollBar.value > 0.13 && scrollBar.value < 0.3)
{
mistEm.enabled = true;
mainMist.simulationSpeed = 1f;
//Change Mist Alpha Back
ourGradient.SetKeys (
new GradientColorKey[] { new GradientColorKey (Color.white, 1.0f), new GradientColorKey (Color.white, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey (0.15f, 0.0f), new GradientAlphaKey (0.0f, 1.0f) });
mistColor.color = ourGradient;
}
else
{
mistEm.enabled = false;
}
//Show Snow based on scroll rect position
if (scrollBar.value > 0.75 && scrollBar.value < 0.94)
{
snowEm.enabled = true;
snowEm2.enabled = true;
}
else
{
snowEm.enabled = false;
snowEm2.enabled = false;
//after disabling snow particle emission I would like to lerp from alpha 255 to 0 to quickly fade out the remaining particles here
}
//Show Flash based on scroll rect position
if (scrollBar.value > 0.96 && scrollBar.value <= 1)
{
flashEm.enabled = true;
}
else
{
flashEm.enabled = false;
}
yield return new WaitForSeconds (2);
}
It is much easier to lerp the _TintColor alpha of the snow material (assu$$anonymous$$g your snow mat uses non-mobile particle alpha blended shader).
They are mobile/particles/additive, but its okay, i found a work around and just commented my solution. I'll definitely remember that though, I didn't know you could change alphas of the materials in the project themselves.
Update:
I just found a work around, after setting particle emission to false i used main.simulationspeed and set it higher so that the remaining particles fade away at a reasonable speed. I can't believe i didn't think of it earlier because I already used main.simulationspeed in this code to slightly change the speed of my mist particle. It doesnt look as good but it worked perfect for another particle i created (sunlight rays). the change in speed is hardly noticed but with the snow its a lil different but itll do for now.
However I am still interested in knowing how you could lerp a gradient alpha key if anyone knows, if not, no big deal.
Your answer
![](https://koobas.hobune.stream/wayback/20220612180702im_/https://answers.unity.com/themes/thub/images/avi.jpg)