You can easily control character health in unity by using a Health Manager in your games
No matter how you portray health visually in your game, having a modular reusable controller for the health value and how it changes is always a good best practice to implement.
In this tutorial series we are going to dive into the concept of how to control character health in unity games with a Health Manager script. This script can be implemented across multiple characters in your game; the players, enemies, and NPCs. It will be a valuable tool in your game design tool box to speed up your development cycle in any game where your characters have health values, regardless of whether they have 1 or 1000 health!
Get the asset pack and follow along
Part 1 - Intro
In part 1 of this series we take a look at why you should use a Health Manager and what type of functionality a Health Manager should contain. We walk through the Health Manager Demo which gives you a visual representation of what it does.
Understandably, your game probably won’t have buttons to damage and heal your characters as shown but the functions that are called when these buttons are pressed are the same functions you would call when your player/enemy gets hit by a sword or bullet.
Part 2 - Variables
In part 2 we start to dig into the basic functionality you may want your health manager to control. We will walk through setting your maximum health, current health, and how to adjust these values to damage and heal your player. We also cover how you can lock the maximum and minimum values so you don’t accidentally give your characters more health than they should have.
Part 3 - Update ( )
The Update ( ) function is a key part to our script. Since Unity calls the update function each and every frame, we layout the framework of our Health Manager within it. All of the primary functions that are needed are called from within Update ( ) in a very specific order.
Part 4 - Get() & Set()
The Get() & Set() functions are used to allow controlled access to private variables inside the Health Manager. These functions will allow other scripts a way to retrieve and update these values without having direct access. This allows better control of these variables and less chance for errors later. It also means easier identification of bugs should any arise.
Part 5 - Lock minimum & maximum health values
In this segment of our Health Manager tutorial series we walk through the LockMinMaxHealth() function which allows us to ensure our characters health never goes below or above the range we have set for it.
Part 6 - Take Damage and Heal
How do you setup a function to take damage or heal your character in Unity? Watch and learn. In this video we continue our Health Manager Tutorial series and take a look at TadeDamage() and Heal() two of the primary functions in our manager. This is how we tell the manager that our character has taken damage or healed and by how much.
Part 7 - Creating a Health Bar in Unity
Creating a health bar in Unity is easy when you know what tools to use. Learn how to create one and how to update it using a health manager to control character health in your game.
Part 8 - Updating the health value text box
Come back for the next installment of this series and learn to update the Health Bar Text box and show your players exactly how much health they have left.