using System.Collections; using System.Collections.Generic; using UnityEngine; public class HealthBar : MonoBehaviour{ public float hp, maxHp = 6f; public bool gameOver; public GameObject gameOverText; public GameObject health1; public GameObject health2; public GameObject health3; public GameObject health4; public GameObject health5; public GameObject health6; public GameObject player; public bool isHealing; GameObject playerO; Animator playerAnim; // Start is called before the first frame update void Start(){ hp = maxHp; playerO = GameObject.FindGameObjectWithTag("Player"); playerAnim = GameObject.FindGameObjectWithTag("Player").GetComponent(); } void Update(){ if (hp == 0f){ gameOver = true; } if (gameOver == true){ gameOverText.SetActive(true); } if (hp == 6){ health1.SetActive(true); health2.SetActive(true); health3.SetActive(true); health4.SetActive(true); health5.SetActive(true); health6.SetActive(true); } if (hp == 5){ health1.SetActive(true); health2.SetActive(true); health3.SetActive(true); health4.SetActive(true); health5.SetActive(true); health6.SetActive(false); } if (hp == 4){ health1.SetActive(true); health2.SetActive(true); health3.SetActive(true); health4.SetActive(true); health5.SetActive(false); health6.SetActive(false); } if (hp == 3){ health1.SetActive(true); health2.SetActive(true); health3.SetActive(true); health4.SetActive(false); health5.SetActive(false); health6.SetActive(false); } if (hp == 2){ health1.SetActive(true); health2.SetActive(true); health3.SetActive(false); health4.SetActive(false); health5.SetActive(false); health6.SetActive(false); } if (hp == 1){ health1.SetActive(true); health2.SetActive(false); health3.SetActive(false); health4.SetActive(false); health5.SetActive(false); health6.SetActive(false); } if (hp == 0){ health1.SetActive(false); health2.SetActive(false); health3.SetActive(false); health4.SetActive(false); health5.SetActive(false); health6.SetActive(false); Destroy(player); } if(isHealing == true){ StartCoroutine(Healing(3f)); playerAnim.SetBool("isHealing", isHealing); } playerAnim.SetBool("isHealing", isHealing); } public void TakeDamage(float amount){ hp = Mathf.Clamp(hp - amount, 0f, maxHp); } IEnumerator Healing(float time){ yield return new WaitForSeconds(time); isHealing = false; } }