-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerHealth.cs
73 lines (50 loc) · 1.15 KB
/
PlayerHealth.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public GameObject deathEffect;
public HealthBar healthbar;
public void Start(){
currentHealth = maxHealth;
healthbar.SetMaxHealth(maxHealth);
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthbar.SetHealth(currentHealth);
StartCoroutine(DamageAnimation());
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
IEnumerator DamageAnimation()
{
SpriteRenderer[] srs = GetComponentsInChildren<SpriteRenderer>();
for (int i = 0; i < 3; i++)
{
foreach (SpriteRenderer sr in srs)
{
Color c = sr.color;
c.a = 0;
sr.color = c;
}
yield return new WaitForSeconds(.1f);
foreach (SpriteRenderer sr in srs)
{
Color c = sr.color;
c.a = 1;
sr.color = c;
}
yield return new WaitForSeconds(.1f);
}
}
}