maxhacker11

HealthManager.cs

Jul 27th, 2023
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.58 KB | Source Code | 0 0
  1. using UnityEngine;
  2.  
  3. // Just a simple health manager to show how you can controll the HealthBar UI
  4. public class HealthManager : MonoBehaviour
  5. {
  6.     public int maxHealth = 100;
  7.     public int currentHealth;
  8.  
  9.     public HealthBar healthBar;
  10.  
  11.     void Start()
  12.     {
  13.         currentHealth = maxHealth;
  14.         healthBar.SetMaxHealth(maxHealth);
  15.     }
  16.  
  17.     void Update()
  18.     {
  19.         // Damage player when we press the G key
  20.         if (Input.GetKeyDown(KeyCode.G))
  21.         {
  22.             TakeDamage(20);
  23.         }
  24.     }
  25.  
  26.     void TakeDamage(int damage)
  27.     {
  28.         currentHealth -= damage;
  29.  
  30.         healthBar.SetCurrentHealth(currentHealth);
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment