Health Regen After X seconds
Trying to get health to regen after being hit. I've tried a few different ways but this is where I get stuck: each time I add functionality for a wait time between getting hit and regaining health. I got it to work at a stage, but then when the player takes, say, 1 damage and a second later 2 damage, it will run either two coroutines in parallel, or just regen health after it took the first amount of damage (1 in this case).
Please help! :P
public class NewPlayerStats : MonoBehaviour {
public int startingHealth = 5;
public int currentHealth;
public int healthReg = 1;
bool isRegenHealth;
void Awake()
{
currentHealth = startingHealth;
}
void Update()
{
if (currentHealth != startingHealth && !isRegenHealth)
{
StartCoroutine(RegainHealthOverTime());
}
}
private IEnumerator RegainHealthOverTime()
{
isRegenHealth = true;
while (currentHealth < startingHealth)
{
Healthregen();
yield return new WaitForSeconds(1);
}
isRegenHealth = false;
}
public void Healthregen()
{
currentHealth += healthReg;
}
}
Answer by Hellium · Jun 21, 2019 at 08:31 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewPlayerStats : MonoBehaviour
{
[SerializeField]
private int maxHealth = 5;
[SerializeField]
private float healthRegenerationRate = 1;
[SerializeField]
private float regenerationDelay = 1;
private int currentHealth;
private float lastDamagesTime = -1;
private float regeneratedHealth;
public bool IsDead
{
get { return currentHealth == 0; }
}
private void Awake()
{
currentHealth = maxHealth;
}
void Update()
{
if ( lastDamagesTime >= 0 && Time.time - lastDamagesTime >= regenerationDelay )
{
RegenerateHealth();
}
}
public void RegenerateHealth()
{
regeneratedHealth += healthRegenerationRate * Time.deltaTime;
int flooredRegeneratedHealth = Mathf.FloorToInt( regeneratedHealth );
regeneratedHealth -= flooredRegeneratedHealth;
Heal( flooredRegeneratedHealth );
}
public void Hurt( int damages )
{
if ( damages < 0 )
throw new System.ArgumentException( "Damages must be greater or equal to 0", nameof( damages ) );
if ( IsDead )
return;
currentHealth = Mathf.Clamp( currentHealth - damages, 0, maxHealth );
regeneratedHealth = 0;
lastDamagesTime = Time.time;
if( IsDead )
{
// Die
}
}
public void Heal( int amount )
{
if ( amount < 0 )
throw new System.ArgumentException( "Healed amount must be greater or equal to 0", nameof( amount ) );
currentHealth = Mathf.Clamp( currentHealth + amount, 0, maxHealth );
if( currentHealth == maxHealth )
{
lastDamagesTime = -1;
regeneratedHealth = 0;
// Full health
}
}
}
Thanks! Although I think there's something wrong in the update method and it's blowing my $$anonymous$$d to be honest.
The player never regains health after taking damage. I tested by creating a start function that uses Damage(2); to instantly hurt the player. He takes the damage but the health never goes up.
Everything works perfectly fine here. The value of regenerationDelay
represents the number of seconds before the health starts to regenerate. Are you sure its value is not too big?
Are you sure the script is enabled? The checkbox in the top-left corner of the script must be checked. Otherwise the Update
won't run. And make sure the gameObject is active.
I made regenerationDelay 1 for testing. Perhaps because I'm instantly applying damage to the player it isnt considering it? I can try using a different way to apply damage and test that. Right now there are no enemies in the game so i'll still need to simulate it some other way