Question by
WyZuxel · Mar 14, 2020 at 11:22 PM ·
scripting problemregenerationhealth
Stopping the player from regenerating health when it takes damage
I've been trying this script for a few hours but i can't get it to work and i have no idea why. The regeneration effect never activates like it was never there.
I'm using this code:
using System.Collections;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public float health;
public float currentHealth;
float maxHealth = 100f;
public GameObject healthBar;
public bool isRegen;
public float regenWaitTime = 2f;
public float regenSpeed = 0.25f;
void Update()
{
if (health > maxHealth) health = maxHealth;
else StartCoroutine(Regen());
}
IEnumerator Regen()
{
yield return new WaitForSeconds(regenWaitTime);
if (currentHealth < maxHealth)
{
currentHealth += 10f;
yield return new WaitForSeconds(regenSpeed);
}
}
public void TakeDamage(float amount)
{
health -= amount;
healthBar.transform.localScale = new Vector3(health / 100, 1f);
if (health <= 0)
{
Destroy(gameObject);
Debug.Log(this.name + " died");
}
StopCoroutine(Regen());
}
}
Comment
Your answer
Follow this Question
Related Questions
Health Regeneration through Photon RPC 1 Answer
does someone have a player health script and a way to make a health bar? 0 Answers
Why is this script not working? 2 Answers
Do Death Function Only Once. 2 Answers
health script not working 0 Answers