Created
February 23, 2019 17:47
-
-
Save JCaraballo113/27ba020094a280cb36e37d740512874c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class HealthSystem : MonoBehaviour | |
{ | |
[SerializeField] int healthPoints = 100; | |
private int maxHealth; | |
[SerializeField] Image healthBar; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
maxHealth = healthPoints; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
public int GetCurrentHealth() | |
{ | |
return healthPoints; | |
} | |
public void Damage(int damage) | |
{ | |
healthPoints -= damage; | |
if (healthBar) | |
{ | |
UpdateHealthBar(); | |
} | |
if (healthPoints < 0) healthPoints = 0; | |
} | |
private void UpdateHealthBar() | |
{ | |
healthBar.fillAmount = (float)healthPoints / (float)maxHealth; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment