Make the color of a plane "flash" using Color32.Lerp?

Hi there,

Would anyone please be able to clarify for me exactly how to use Color32.Lerp?

I have it so that it will lerp from a fully transparent plane to half transparent over x amount of time. So I get that. But what say I wanted to flash the plane on and then off on some input?

Thanks all!

Lerp is short for ‘linear interpolation’.

Its formula is:

f(a,b,t) = (b - a) * t + a

What it basically does is just find the value ‘t’ percent between a and b.

a and b just have to be values that define the arithmetic operators of:

subtraction, addition, scalar multiplication

This includes:

numbers (floats, doubles, ints), vectors (Vector2, Vector3, Vector4), non-euclidian vectors (Color32, Color, etc), and many other things.

That’s ALL lerp is.

If you say:

lerp(a,b,0.5) is half way between a and b
lerp(a,b,0.25) is quarter way from a to b
lerp(a,b,1f) is b, because b is 100% of the way from a to b.

that’s it.

NOTE - unities built in lerp methods clamp ‘t’ from 0->1, despite the fact the formula works correctly for values outside that range. lerp(a,b,2f) would be 200% the distance from a to b. But in unity it’d just give you b, since it’s clamped. Use the ‘unclampedLerp’ methods to NOT clamp values.

SO, lerping just the alpha channel of a colour? Really, you could have just Mathf.Lerped the alpha channel, rather than the whole color.

Lerping a colour to turn it on and off? That’s not what lerp does! Lerp translates from one value to another by some percentage. You’re desire is to binary operate it on and off. And doing this with a transparency layer probably isn’t a good idea as it’s more expensive… it’s easier to literally enable/disable the renderer.

That is of course unless you wanted to have a transition duration from off to on, where it fades in and out. Then a transparency would be needed. This can be done with a lerp call every frame, BUT that results in a logarithmic curve (and not linear). If you want other shaped curves, you need to look to other algorithms.

The most common one is an ‘ease’:

And often paired with a tween engine. Or can be manually updated in a Coroutine.

Here’s a simple example of a routine easing a position:

IEnumerator EaseRoutine(Transform targ, Ease ease, Vector3 start, Vector3 end, float dur)
{
    float t = 0f;
 
    while(t < dur)
    {
        var dt = ease(t, 0f, 1f, dur);
        //note the use of lerp here
        //the ease returned a percent from 0 to 1, start to finish, that we are
        //so we lerp that percent from start to end
        var pos = Vector3.Lerp(start, end, dt);
        targ.position = pos;
     
        yield return null;
        t += Time.deltaTime;
    }
 
    targ.position = end;
}

Note how this allows us to control, start, end, and duration of the entire ease. Unlike just a general old lerp.

Thank you for the elaborate answer :slight_smile:

I did indeed have a desire to fade in and out rather than just binary, so it was the alpha channel which I needed. Seeing as you can’t modify the channels of a Color directly, isn’t it technically more efficient just to create the colors (start - end for example), and lerp between them based on adding deltaTime, then set the object’s material’s color to the lerps value?

This is what I ended up doing:

if (photoSnapIn) {

                aTimer += Time.deltaTime / flashTime;

                lerpedColor = Color32.Lerp (transWhite, visibleWhite, aTimer);

                thePlane.GetComponent<Renderer> ().material.color = lerpedColor;

                if (lerpedColor.a == visibleWhite.a)
                {
                    photoSnapIn = false;
                    aTimer = 0.0f;
                    photoSnapOut = true;
                }
            }

            if (photoSnapOut)
            {
                aTimer += Time.deltaTime / flashTime;

                lerpedColor = Color32.Lerp (visibleWhite, transWhite , aTimer);
              
                thePlane.GetComponent<Renderer> ().material.color = lerpedColor;
              
                if (lerpedColor.a == visibleWhite.a)
                {
                    photoSnapOut = false;
                    aTimer = 0.0f;
                }
            }