Advertisement
dronkowitz

Health.cs

May 18th, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Health : MonoBehaviour
  6. {
  7.     private int health = 3;
  8.     private float timeSinceLastHit = 5;
  9.     public bool dead;
  10.     public HealthBar bar;
  11.     // Start is called before the first frame update
  12.     void Start()
  13.     {
  14.  
  15.     }
  16.  
  17.     // Update is called once per frame
  18.     void Update()
  19.     {
  20.  
  21.     }
  22.     public void TakeDamage(int damage)
  23.     {
  24.         if (dead) return;
  25.         GetComponentInChildren<Animator>().SetBool("Sleep", false);
  26.         health = Mathf.Max(0, health - damage);
  27.         bar.Hit(health);
  28.         if (health == 0)
  29.         {
  30.             dead = true;
  31.             GetComponentInChildren<Animator>().SetTrigger("Die");
  32.             GetComponent<Collider>().enabled = false;
  33.             FindObjectOfType<HUD>().AddPower(10);
  34.             Destroy(gameObject, 4);
  35.  
  36.         }
  37.  
  38.     }
  39.  
  40.     private void OnCollisionEnter(Collision collision)
  41.     {
  42.         if (collision.transform.CompareTag("Player")) TakeDamage(1);
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement