blob: 38e354ae30b585f4301e9d55a3612fa74104d94a (
plain)
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
|
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealthScript : MonoBehaviour {
public float Health;
public float MaxHealth = 100;
bool Damaged;
//HUD
GameObject HUD;
Text HealthText;
Text GameOverText;
Image DamageIndicator;
void Start ()
{
HUD = GameObject.FindGameObjectWithTag("HUD");
HealthText = HUD.transform.Find("HealthText").GetComponent<Text>();
GameOverText = HUD.transform.Find("GameOverText").GetComponent<Text>();
DamageIndicator = HUD.transform.Find("DamageIndicatorImage").GetComponent<Image>();
Health = 100;
HealthText.text = Health + "/100";
}
void Update ()
{
//Damage Animation
if (Damaged)
{
DamageIndicator.color = new Color(1, 0, 0, .1f);
}
else
{
DamageIndicator.color = Color.Lerp(DamageIndicator.color, Color.clear, 1f * Time.deltaTime);
}
Damaged = false;
}
public void TakeDamage(float damage)
{
Health -= damage;
if (Health <= 0) Health = 0;
HealthText.text = Health + "/100";
Damaged = true;
if (Health <= 0)
{
Health = 0;
gameObject.GetComponent<PlayerMovementScript>().enabled = false;
gameObject.GetComponentInChildren<PlayerShootingScript>().enabled = false;
StartCoroutine(Restart());
}
}
IEnumerator Restart()
{
GameOverText.text = "Game Over";
yield return new WaitForSeconds(3f);
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name);
}
}
|