simple color lerp...isn't lerping!

This should be pretty cut and dry. I’m still a beginner, but I’ve gotten translation lerps to work. But all this color lerp wants to do is stay on the color and nerver lerp back.

var normalColor 					: Color = Color.white;
var hitColor          				: Color = Color.red;

var hitFlash						: float = 0.5f;



function FlashHit ()
{
	renderer.material.color = hitColor;
	
	renderer.material.color = Color.Lerp (hitColor, normalColor, hitFlash);
}

I’m expecting on execution of this function for the Object to instantly pop to hit color, then lerp back to normalcolor after .5 seconds. What I get is the object instantly flashes to the hit color, but never lerps back to normal color. Like the lerp isn’t there.

Any ideas as to how I’m screwing this up?

Aren’t you supposed to Lerp inside Update? :slight_smile: - If you’re calling FlashHit inside Update, then the color would reset every time to hitColor, Lerp wouldn’t be able to do its job effectively.

I was writing you and example when I found this, then I stopped.

I started writing an example, but then I noticed that vexe (who was also writing an example), found the nice post. At first I thought to discard my writings, but then I decided to post it as alternative, coroutine based, solution :slight_smile:

function Update ()
{
    if(Input.GetKeyDown(KeyCode.Space))
    {
        FlashHit();
    }
}
 
function FlashHit ()
{
    renderer.material.color = hitColor;

    var t : float = 0;
    while(t < 1)
    {
        t += Time.deltaTime / hitFlash;
        renderer.material.color = Color.Lerp (hitColor, normalColor, t);
        yield;
    }
}