Gameplay.HealthSystem - robblofield/TomeboundDocs GitHub Wiki
Author: Eric Fernandes
Last Updated: 07/12/2023
Overview
A basic health system that is linked to the UI which displays five hearts and their containers, along with simple heal and take damage functions.
Dependencies
- Heal Script
- Enemy Script
- GameManager Script
Contents
- Use Case
- Breakdown of Code
- Using Statements
- Class Declaration
- Variables
- Current Health [Int]
- Max Health [Int]
- numOfHearts [Int]
- Image[] hearts
- Sprite fullHeart
- Sprite emptyHeart
- Start()
- Update()
- TakeDamage()
- Heal()
- Future Expansion
- Full Code Reference
Use Case
This script would be applied to the main characters/player objects and it would function by using the tags and trigger system of the Unity Engine, by allowing the player to take damage from interacting/colliding with enemy objects and heal for when colliding with healing objects. It would be further used for possible items that would provide benefits aside from healing, such as invulnerability.
Breakdown of Code
Using Statements
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
The script uses the scene management in order to reload the same scene for when the player reaches 0 health, resulting in a game-over.
Class Declaration
public class Health : MonoBehaviour
Variables
public int maxHealth = 5;
public int currentHealth;
public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
private bool isDead;
Int maxHealth: The max value the integer can go to
Int currentHealth: The current value/health the object has.
Int numOfHearts: The value to determine how many hearts are on display.
Image hearts: The image that the UI uses to display the amount of hearts.
Sprite fullHeart: The specific sprite used to show how much health the player has through total hearts.
Sprite emptyHeart: The specific sprite used to show how many hearts the player has lost.
isDead: When the current health value reaches 0, it will set this boolean to true and trigger the game over screen.
Start()
// Start is called before the first frame update
public void Start()
{
currentHealth = maxHealth;
}
This makes the current health value be equal to the max health value at the start of the game's run time.
Update()
void Update()
{
if(currentHealth > numOfHearts)
{
currentHealth= numOfHearts;
}
for (int i = 0; i < hearts.Length; i++)
{
if(i < currentHealth)
{
hearts[i].sprite = fullHeart;
}
else
{
hearts[i].sprite = emptyHeart;
}
if(i <numOfHearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].enabled = false;
}
}
}
The first part is checking if the current health is greater than the number of hearts, and if it matches the requirement, then it would make both values be equal.
The next part is specifically for the distance between each heart, along with going through each value in the current health integer to check what the value is and if the integer for number of hearts is lower than current health.
If a player loses health and the current health value is lowered by one, the update function will check for it in the update and replace the full heart sprite with an empty heart sprite to show the player has lost health.
Take Damage()
public void TakeDamage(int amount)
{
currentHealth -= amount;
if(currentHealth <= 0 && !isDead)
{
gameObject.SetActive(false);
isDead = true;
gameManager.gameOver();
print("You Died!");
}
}
The function is for the player to 'take damage', which is lowering the current health integer by one with each interaction with the object's trigger. [The interaction/trigger detection is in a different script, 'Enemy.cs']
Heal()
public void Heal(int amount)
{
currentHealth += amount;
if (currentHealth > maxHealth)
{
currentHealth = maxHealth;
}
}
The function is for the player to heal after taking damage, which is increasing the int value by one upon interacting with the object with the specific tag. It also detects if the player has already reached max health, and with that, the heal cannot make the player have more health then they are meant to. [The interaction/trigger detection is in a different script, 'Heal.cs']
Future Expansion
There are plans to expand upon the health script by reducing the total amount of scripts needed for this to work, bringing in the functions from the different scripts and having it all through this singular script through the use of tags and extra detection methods.
Full Code Reference
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
public int maxHealth = 5;
public int currentHealth;
public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
private bool isDead;
public GameManagerScript gameManager;
// Start is called before the first frame update
public void Start()
{
currentHealth = maxHealth;
}
void Update()
{
if(currentHealth > numOfHearts)
{
currentHealth= numOfHearts;
}
for (int i = 0; i < hearts.Length; i++)
{
if(i < currentHealth)
{
hearts[i].sprite = fullHeart;
}
else
{
hearts[i].sprite = emptyHeart;
}
if(i <numOfHearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].enabled = false;
}
}
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if(currentHealth <= 0 && !isDead)
{
gameObject.SetActive(false);
isDead = true;
gameManager.gameOver();
print("You Died!");
}
}
//healing system
public void Heal(int amount)
{
currentHealth += amount;
if (currentHealth > maxHealth)
{
currentHealth = maxHealth;
}
}
}