Lerp between 3 colors based on energy amount.

Hi all, I have a simple gradient texture I’m using for an energy bar. I start with full energy, and green color for the bar. I can’t quite figure out Color.Lerp. I want to fade from green, to yellow, to red as the energy runs down, and smoothly back to green as energy increases. I can get color to fade to another, but can’t quite come up w/ a proper formula to address my needs…

You just need to split it up into 2 different Lerps:

if (health < .5) { 
    blendColor = Color.Lerp (Color.red, Color.yellow, health*2);
    } else {
    blendColor = Color.Lerp (Color.yellow, Color.green, (health-.5)*2);
    }

The basic pseudo code would be:

var maxEnergy : flaot;
var energy : float;
var greenColor : Color;
var redColor : Color;

Color.Lerp(redColor, greenColor, energy /maxEnergy);

Though you will need to put the last line in a method, presumably the Update function.

Scribe

EDIT:

just reread your question and completely missed the 3rd color part, it was 1 in the morning :S

@Kiloblargh’s answer is perfect (you could replace health with the energy/maxEnergy)

Sorry about that!