Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class PlayerHealth : MonoBehaviour
  5. {
  6.     [SerializeField]
  7.     Slider healthBar;
  8.     [SerializeField]
  9.     Text healthText;
  10.     [SerializeField]
  11.     GameObject DeathUI;
  12.  
  13.     public Animator anim;
  14.  
  15.     public float maxHealth = 100;
  16.     public float curHealth;
  17.  
  18.     void Start()
  19.     {
  20.         anim = GetComponent<Animator>();
  21.  
  22.         healthBar.value = maxHealth;
  23.         curHealth = healthBar.value;
  24.     }
  25.  
  26.     void OnTriggerStay2D(Collider2D col)
  27.     {
  28.         if (col.gameObject.tag == "Saw")
  29.         {
  30.             healthBar.value -= .5f;
  31.             curHealth = healthBar.value;
  32.         }
  33.         if (col.gameObject.tag == "Acid")
  34.         {
  35.             healthBar.value -= .2f;
  36.             curHealth = healthBar.value;
  37.         }
  38.         if (col.gameObject.tag == "Spikes")
  39.         {
  40.             healthBar.value -= .2f;
  41.             curHealth = healthBar.value;
  42.         }
  43.     }
  44.  
  45.     void Update()
  46.     {
  47.         //n0 means do not show any decimal places
  48.         healthText.text = curHealth.ToString("n0") + " %";
  49.  
  50.         //keep health bar value up to date with the
  51.         //players health value
  52.         healthBar.value = curHealth;
  53.  
  54.         if (curHealth <= 0)
  55.         {
  56.             //play death animation
  57.             anim.SetBool("isDead", true);
  58.  
  59.             //stop all player movement
  60.             GetComponent<RobotController>().enabled = false;
  61.  
  62.             //enables the death UI
  63.             DeathUI.gameObject.SetActive(true);
  64.         }
  65.  
  66.         //check to make sure our current health
  67.         //doesn't go over our max health
  68.         if (curHealth >= maxHealth)
  69.             curHealth = maxHealth;
  70.            
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement