-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealth.cs
46 lines (39 loc) · 1021 Bytes
/
Health.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using UnityEngine;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
public int MaxHealth;
private int CurrentHealth;
public Slider healthBar;
public AudioClip dieSound;
private AudioSource audioSource;
public GameObject DieScreen;
private void Start() {
audioSource = GetComponent<AudioSource>();
CurrentHealth = MaxHealth;
healthBar.value =CurrentHealth;
}
public void ChangeHealth(int amount)
{
CurrentHealth += amount;
healthBar.value =CurrentHealth;
if (CurrentHealth <= 0)
{
CurrentHealth = 0;
healthBar.value =CurrentHealth;
// Die
Die();
}
if (CurrentHealth > MaxHealth)
{
CurrentHealth = MaxHealth;
healthBar.value =CurrentHealth;
}
}
public void Die()
{
DieScreen.SetActive(true);
audioSource.clip = dieSound;
audioSource.PlayOneShot(audioSource.clip);
}
}