Shields recovery time

Hi,
I’m trying to get my shields to recover when damaged at a rate of 1 point per X seconds. I have it working to good, they recover almost instantly. I’ve tried variations of the script but can’t get it to go off every second.
Here’s what i have now. I’ve tried lots of different options but it’s always recovering almost instantly. “damaged” is a boolean that is true when the shields are below max shields.

function Update ()
{
Percent=shields/maxShields;
if (shields<maxShields)
{
repair();
damaged=true;
}
else
{
damaged=false;
}

}


function repair()
{
while(damaged)
{
shields++;
yield WaitForSeconds(3);
}
}

I’m not sure exactly what you want: do you want it to “pulse” heal once every second? Or do you want it to heal smoothly, but slower?

// PULSE HEAL
// You could use a Coroutine - I don't know the Coroutine syntax for JavaScript though so someone else would have to help with that
// Alternatively, you could use the Update loop with a timer

var shieldPulse : float = 0;
var repairRate : float = 15;

fnction Update () {
	shieldPulse -= Time.deltaTime;
	if (shieldPulse <= 0){
		shieldPulse = 1.0f; // Or however many seconds you want between pulses
		Repair();
	}
}

function Repair(){
	shields = Mathf.Clamp(shields + repairRate, 0, maxShields);
}

// If you know how to do a Coroutine you could use one of them - I'm sure someone could convert this to JS or offer an alternative Coroutine in JS for you though
void Start () {
	StartCoroutine(Repair());
}

IEnumerator Repair() {
	while (true){
		shields = Mathf.Clamp(shields + repairRate, 0, maxShields);
		yield return new WaitForSeconds(1);
	}
}


// SMOOTH, SLOW HEAL

var repairRate : float = 15;

function Update () {
	shields = Mathf.Clamp(shields + (repairRate * Time.deltaTime), 0, maxShields);
}

Either way, the takeaway is that you need to take Time.deltaTime into consideration somewhere (the coroutine handles time for you with the WaitForSeconds). With the // SMOOTH, SLOW HEAL method - just tweak the repairRate variable until it repairs at a rate you find appropriate for your needs.

Thanks buddy, the smooth slow heal was exactly what I’m looking for.