From 115f77bda0246a00f6e17469685c67746bdbd29d Mon Sep 17 00:00:00 2001 From: nirav Date: Sat, 12 Dec 2020 07:18:36 +0000 Subject: Initial commit --- Assets/Scripts/Player/PlayerHealthScript.cs | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 Assets/Scripts/Player/PlayerHealthScript.cs (limited to 'Assets/Scripts/Player/PlayerHealthScript.cs') diff --git a/Assets/Scripts/Player/PlayerHealthScript.cs b/Assets/Scripts/Player/PlayerHealthScript.cs new file mode 100755 index 0000000..38e354a --- /dev/null +++ b/Assets/Scripts/Player/PlayerHealthScript.cs @@ -0,0 +1,66 @@ +using UnityEngine; +using System.Collections; +using UnityEngine.UI; +using UnityEngine.SceneManagement; + +public class PlayerHealthScript : MonoBehaviour { + + public float Health; + public float MaxHealth = 100; + bool Damaged; + + //HUD + GameObject HUD; + Text HealthText; + Text GameOverText; + Image DamageIndicator; + + void Start () + { + HUD = GameObject.FindGameObjectWithTag("HUD"); + HealthText = HUD.transform.Find("HealthText").GetComponent(); + GameOverText = HUD.transform.Find("GameOverText").GetComponent(); + DamageIndicator = HUD.transform.Find("DamageIndicatorImage").GetComponent(); + Health = 100; + HealthText.text = Health + "/100"; + } + + + void Update () + { + //Damage Animation + if (Damaged) + { + DamageIndicator.color = new Color(1, 0, 0, .1f); + } + else + { + DamageIndicator.color = Color.Lerp(DamageIndicator.color, Color.clear, 1f * Time.deltaTime); + } + Damaged = false; + } + + public void TakeDamage(float damage) + { + Health -= damage; + if (Health <= 0) Health = 0; + HealthText.text = Health + "/100"; + + Damaged = true; + + if (Health <= 0) + { + Health = 0; + gameObject.GetComponent().enabled = false; + gameObject.GetComponentInChildren().enabled = false; + StartCoroutine(Restart()); + } + } + + IEnumerator Restart() + { + GameOverText.text = "Game Over"; + yield return new WaitForSeconds(3f); + SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name); + } +} -- cgit v1.2.3