dronkowitz

HealthBar.cs

May 18th, 2021 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class HealthBar : MonoBehaviour
  6. {
  7.     private float timeSinceLastHit = 6;
  8.  
  9.     public Sprite[] bars = new Sprite[3];
  10.     public SpriteRenderer bar;
  11.  
  12.     // Update is called once per frame
  13.     void Update()
  14.     {
  15.         timeSinceLastHit -= Time.deltaTime;
  16.  
  17.         if (timeSinceLastHit <= 0)
  18.         {
  19.             bar.enabled = false;
  20.         }
  21.         transform.LookAt(Camera.main.transform);
  22.     }
  23.  
  24.     public void Hit(int health)
  25.     {
  26.         timeSinceLastHit = 5;
  27.         bar.enabled = true;
  28.         bar.sprite = bars[health];
  29.     }
  30.  
  31. }
  32.  
  33.  
Add Comment
Please, Sign In to add comment