Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- public class PlayerHealth : MonoBehaviour
- {
- [SerializeField]
- Slider healthBar;
- [SerializeField]
- Text healthText;
- [SerializeField]
- GameObject DeathUI;
- public Animator anim;
- public float maxHealth = 100;
- public float curHealth;
- void Start()
- {
- anim = GetComponent<Animator>();
- healthBar.value = maxHealth;
- curHealth = healthBar.value;
- }
- void OnTriggerStay2D(Collider2D col)
- {
- if (col.gameObject.tag == "Saw")
- {
- healthBar.value -= .5f;
- curHealth = healthBar.value;
- }
- if (col.gameObject.tag == "Acid")
- {
- healthBar.value -= .2f;
- curHealth = healthBar.value;
- }
- if (col.gameObject.tag == "Spikes")
- {
- healthBar.value -= .2f;
- curHealth = healthBar.value;
- }
- }
- void Update()
- {
- //n0 means do not show any decimal places
- healthText.text = curHealth.ToString("n0") + " %";
- //keep health bar value up to date with the
- //players health value
- healthBar.value = curHealth;
- if (curHealth <= 0)
- {
- //play death animation
- anim.SetBool("isDead", true);
- //stop all player movement
- GetComponent<RobotController>().enabled = false;
- //enables the death UI
- DeathUI.gameObject.SetActive(true);
- }
- //check to make sure our current health
- //doesn't go over our max health
- if (curHealth >= maxHealth)
- curHealth = maxHealth;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement