Skip to content

Instantly share code, notes, and snippets.

@GameDevMix
Last active January 27, 2024 13:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GameDevMix/9e2af823ecc0995df369ca7e6fe8012e to your computer and use it in GitHub Desktop.
Save GameDevMix/9e2af823ecc0995df369ca7e6fe8012e to your computer and use it in GitHub Desktop.
A simple health and death controller for a player character in Unity
using UnityEngine;
// A simple health controller for a player in Unity
public class PlayerHealthController : MonoBehaviour
{
#region Properties
// The player's health at the start
[SerializeField] private int healthInitial = 3;
// The player's health right now
private int healthCurrent;
#endregion
#region Initialisation methods
// Initialises this component
// (NB: Is called automatically before the first frame update)
void Start()
{
// Initialiase the player's current health
ResetHealth();
}
// Sets the player's current health back to its initial value
public void ResetHealth()
{
// Reset the player's current health
healthCurrent = healthInitial;
}
#endregion
#region Gameplay methods
// Reduces the player's current health
// (NB: Call this if hit by enemy, activated trap, etc)
public void TakeDamage(int damageAmount)
{
// Deduct the provided damage amount from the player's current health
healthCurrent -= damageAmount;
// If the player has no health left now
if (healthCurrent <= 0)
{
// Kill the player
Destroy(gameObject);
// NB: Here, you may want to restart the game
// (e.g. by informing your game manager that the player has died,
// or by raising an event using your event system)
}
}
// Increase the player's current health
// (NB: Call this if picked up potion, herb, etc)
public void Heal(int healAmount)
{
// Add the provided heal amount to the player's current health
healthCurrent += healAmount;
// If the player has too much health now
if (healthCurrent > healthInitial)
{
// Reset the player's current health
ResetHealth();
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment