Animate changing health bar colours

I’m creating a game for a Uni assignment and want to recreate the Socom 2 style health bar.

For reference: http://www.youtube.com/watch?v=ANFjPvRNywo go to the 0:56 mark. I would advise muting the audio (btw its not my video, just one I have used for reference)

I have coded most of it, getting shot changes damage to yellow then after a few seconds it goes red. It works great but instead of swapping the colours straight away I want to animate the change, so that the yellow bar will decrease as the red bar increases.

I calculate the health remaining, health lost and damage done and the length of each coloured bar changes accordingly so there is no overlay of the textures, as they are opaque.

Is there a specific loop I can use which will decrease the yellow bar value as the red bar increases? How would one recommend I go about achieving this?

Thanks for any help.

Can’t watch the video because I’m at work - but you can use Mathf.SmoothDamp to interpolate between the values. Example (untested code):

private double max_health = 100;
private double health;
private double smoothTime = .3;
private float vel = 0.0f;

var healthbar;
var damagebar;


public void init() {
    health  = max_health;
}

public void hit(float damage)
{
   health-=damage;
}

public void update()
{
    healthbar.value = Mathf.SmoothDamp(healthbar.value, health, vel, smoothTime);
    damagebar.value = Mathf.SmoothDamp(damagebar.value, maxHealth - health, vel, smoothTime);
}

Something like that should work - there’s a number of other functions in Mathf that do similar things with slightly different results (such as lerp).