health regen help

Hi all,

I have this script where i have an aoe giving out damage which increases the closer you get.

i then have the damage reciever script that changes the hitpoints according to the damage it takes.

what i want to do is add a regen varible that will continuously regenerate the hitpoints at a rate which is defined in the varible. i.e 1 health a second.

Its also important that the health wont regenerate higher that the original hitpoints.

here is the script i have so far which is attached to the main player, its made from following the fps tutorial. Ive added the regen var in ready but thats as far as i got as i couldnt work out what to add to make it work.

var hitPoints = 100.0;
var regen = 1;

function ApplyDamage (damage : float) {
    // We already have less than 0 hitpoints, maybe we got killed already?
    if (hitPoints <= 0.0)
        return;

    hitPoints -= damage;

}

@script RequireComponent (Rigidbody)

function OnGUI () {
    GUILayout.Box ("hitPoints " + Mathf.Round(hitPoints));
    GUILayout.Box ("regen " + Mathf.Round(regen));

}

I display the hitpoints and regen so i can see it working on runtime.

Thank you in advance for any help.

Marquis

This function will add to hitPoints, only while under 100, and will be at a constant rate thanks to Time.deltaTime (which is the amount of time in seconds since the last update loop).

function Update() {
    if(hitPoints <= 100) {
        hitPoints += regen * Time.deltaTime;
    }
}