- Home /
Health Bar Lerp
Hey Guys and Girls,
I was wondering if anyone can help with this issue, I am trying to lerp a color from green to red the lower the hp gets.
I did this before with a fuel amount script, but I can't get it to work here, would someone mind looking at this and telling me whats wrong?
var hp : float;
var maxHp : float;
var perc : float;
var healthBar : GUITexture;
function Start ()
{
perc = (hp/maxHp)*100;
}
function Update ()
{
healthBar.color = Color.Lerp(Color.green, Color.red,hp * .01);
}
function OnTriggerEnter(other:Collider)
{
}
function OnTriggerExit()
{
}
function OnMouseDown()
{
hp-=12;
if(hp<0)
hp = 0;
perc = (hp/maxHp)*100;
perc = Mathf.Round(perc);
healthBar.pixelInset.width = perc;
}
Thanks
I'll just add with this script, that the color currently switches instantly from green to red. Regardless of hp.
Answer by HappyMoo · Jan 01, 2014 at 03:08 PM
Does this do what you want? I'm not sure about pixelInsect as that may behave strangely if you have a scale applied somewhere.
var hp : float;
var maxHp : float;
var healthBar : GUITexture;
function Update ()
{
var ratio:float = hp/maxHp;
healthBar.pixelInset.width = ratio*healthBar.texture.width;
healthBar.color = Color.Lerp(Color.red, Color.green, ratio);
}
function OnMouseDown()
{
hp-=12;
if(hp<0) hp = 0;
}
Your answer
Follow this Question
Related Questions
Lerp Color doesn't work 2 Answers
Healthbar Color change movement 1 Answer
HealthBar with ColorChange 1 Answer
How do I loop a Color32.Lerp? 3 Answers
How to make two colors lerp independently in this code? 1 Answer