Color.Lerp how does it work?

Hello there

I’m trying to make a script to fade the screen to black when the scene ends, I’m trying to use Color.Lerp (as the tutorial Stealth says it has to be done) This is my code:

public void EndScene(){
        guiTexture.enabled = true;
        guiTexture.color = Color.Lerp (guiTexture.color,Color.black,fadeSpeed*Time.deltaTime);
        Debug.Log(guiTexture.color.a);
        if (guiTexture.color.a >= 0.95f) {
            guiTexture.color=Color.black;
            Application.LoadLevel(0);
        }
    }

I call the EndScene Function from the Update function of another Script, however I have debugged and what is happening is that the code never enter in the IF statement, I have printed guiTexture.color.a and it only gets random numbers between 0.001 and 0.060, or so, the point is it never reaches 0.95 so it never enters in the IF block. So I’m wondering how does Color.Lerp works? Am I missing another parameter or something? Why does it never reach the value I need?

Thanks for reading!

Can someone explain how using Time.deltaTime as 't' in a lerp actually works? - Questions & Answers - Unity Discussions Typically it’s simpler to use Lerp in a coroutine.

–Eric

1 Like

Do remember that the “t” parameter to all lerp functions is basically a percentage (from 0 to 1.) It doesn’t really have anything to do with time, though you can vary the parameter’s value over time, of course, which makes sense for animations.

In your case, Time.deltaTime is the time since the last frame, which is usually a very small value. As such, you won’t see much of a difference from frame to frame.

It’s also mathematically incorrect and will result in different behavior at different framerates. (That is, the usage of Time.deltaTime like that in Lerp, not Time.deltaTime itself.)

–Eric