-
Notifications
You must be signed in to change notification settings - Fork 5
/
PlayerStatus.cs
43 lines (37 loc) · 1.07 KB
/
PlayerStatus.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
using UnityEngine;
namespace Section.Methods.Challenge02
{
public class PlayerStatus : MonoBehaviour
{
/// Challenge Overview:
/// Create a program that checks whether the player is dead or alive.
/// On space key press, damage the player by a random amount.
/// If the player is dead, print "The player has died!"
///
/// Bonus: Prevent the method from being called if the player is dead
/// (no negative health values)
///
public int health = 100;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && IsDead() == false)
{
Damage(Random.Range(5, 20));
}
}
public void Damage(int damageAmount)
{
health -= damageAmount;
if (IsDead())
{
health = 0;
Debug.Log("The player has died!");
}
}
public bool IsDead()
{
return health < 1;
}
}
}