- Home /
Health Regen Stop After Damage
I'm working on a health regen system for a game and I'm running into some issues. Seems no one else has tried this as far as I can tell so I HAVE Googled it already.
Anyway I want the system to act like this: When damage is received, wait x seconds and and then start the regen process. If damage is received WHILE regening, stop the regen process and start over.
I can't quite figure out how exactly to do this. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthScript : MonoBehaviour {
int maxHealth = 100; //Player's Max health
public float currentHealth; //Player's Current health
public float regenSpeed; //Regen speed (wait time)
public float regenWaitTime; //Time before regen starts
bool isRegen = false; //If regening
bool canRegen = true; //If can regen
void Start(){
currentHealth = maxHealth; //Sets player health
}
void Update () {
if(currentHealth>maxHealth){
currentHealth = maxHealth;
}else if(currentHealth!=maxHealth && !isRegen){ //If they have health less than max
StartCoroutine(Regen()); //Start regen
}
}
//Function for health regen
IEnumerator Regen(){
isRegen = true; //Set regenning to true
yield return new WaitForSeconds(regenWaitTime); //Wait for delay
while(currentHealth<maxHealth){ //Start the regen cycle
currentHealth += 1; //Increase health by 1
yield return new WaitForSeconds(regenSpeed); //Wait for regen speed
}
isRegen = false; //Set regenning to false
}
//Function for dying
void Die(){
Debug.Log(this.name+" died."); //This is where death cycle will go
canRegen = false;
}
//Function for taking damage
public void TakeDamage(float damage){
if(currentHealth<=0){
Die();
}else{
currentHealth -= damage; //Remove health
}
}
}
Answer by ShadyProductions · Aug 03, 2017 at 07:08 AM
You can use StopCoroutine for this, However you will have to store the coroutine in order to stop it, because calling Regen will always return a new IEnumerator. So it isn't the same when u do StopCoroutine(Regen()); and it won't work. You will have to save and use it like so:
IEnumerator couroutine = Regen();
StopCoroutine(couroutine);
// Actual code:
private Coroutine coroutine;
void Update()
{
if (currentHealth > maxHealth)
{
currentHealth = maxHealth;
}
else if (currentHealth != maxHealth && !isRegen)
{ //If they have health less than max
coroutine = Regen();
StartCoroutine(coroutine); //Start regen
}
}
//Function for taking damage
public void TakeDamage(float damage)
{
if (isRegen)
{
StopCoroutine(couroutine);
isRegen = !isRegen;
}
if (currentHealth <= 0)
{
Die();
}
else
{
currentHealth -= damage; //Remove health
}
}
Thank you so much! I've been sitting here trying to figure this out for at least a good few hours. Finally have a solution lol!
Now it wasn't that hard now that you see the solution right?:P
Yeah. I looked at the stopcoroutine thing but didn't do it right so I figured it just didn't work
Your answer
Follow this Question
Related Questions
Health below zero but shows negative numbers 1 Answer
Health Bar going 0 right away 1 Answer
Help w/variables 0 Answers
Health/Damage system 1 Answer
Health Code, Damage and Healing 0 Answers