maxhacker11

HealthManager.cs

Nov 11th, 2023
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class HealthManager : MonoBehaviour
  6. {
  7.     public int maxHealth = 100;
  8.     public int currentHealth;
  9.  
  10.     public HealthBar healthBar;
  11.     public GameObject bloodEffect;
  12.  
  13.     private void Start()
  14.     {
  15.         currentHealth = maxHealth;
  16.         healthBar.SetMaxHealth(maxHealth);
  17.     }
  18.  
  19.  
  20.     public void TakeDamage(int damage, Vector2 origin)
  21.     {
  22.         currentHealth -= damage;
  23.  
  24.         // Blood Particle example
  25.         Instantiate(bloodEffect, transform.position, Quaternion.identity);
  26.        
  27.         // Camera shake code example
  28.         //CameraShake.instance.Shake();
  29.  
  30.         // Knockback code example
  31.         //GetComponent<Rigidbody2D>().AddForce((GetComponent<Rigidbody2D>().position - origin).normalized * 500f, ForceMode2D.Force);
  32.  
  33.         if (currentHealth <= 0)
  34.         {
  35.             Destroy(gameObject);
  36.         }
  37.  
  38.         healthBar.SetCurrentHealth(currentHealth);
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment