-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDamageVisual.cs
More file actions
48 lines (42 loc) · 1.31 KB
/
Copy pathDamageVisual.cs
File metadata and controls
48 lines (42 loc) · 1.31 KB
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
using UnityEngine;
public class DamageVisual: MonoBehaviour {
static Material damageMat {
get {
if (_damageMat == null) {
_damageMat = Resources.Load<Material>("Damage-Flash");
if (_damageMat == null) Debug.LogError("Damage material not found in resources folder!");
}
return _damageMat;
}
}
static Material _damageMat;
[SerializeField] Renderer[] meshes;
Material[] defaultMats;
Health health;
void Awake() {
if (TryGetComponent<Health>(out var health)) {
this.health = health;
} else {
var inParent = GetComponentInParent<Health>();
if (inParent != null) health = inParent;
else Debug.LogError("Couldn't find health component!");
}
defaultMats = new Material[meshes.Length];
for (int i = 0; i < meshes.Length; i++) {
defaultMats[i] = meshes[i].material;
}
}
void Start() {
health.OnDamage += OnDamage;
}
void OnDamage() {
foreach (var mesh in meshes) {
mesh.material = damageMat;
}
new Timeout(() => {
for (int i = 0; i < meshes.Length; i++) {
meshes[i].material = defaultMats[i];
}
}, 0.2f, this);
}
}