Creating Health Systems

Kenny Pruitt
Unity Coder Corner
Published in
3 min readMar 1, 2023

Objective: Create Player & Enemy Health Systems

Creating health and damage systems are very easy. On the enemy and player I just need to add a current health and max health floats. And on the player I will also add a bool to tell if the player is dead.

Then I just need to add a function that other scripts can access to adjust the health. I will also require a float to be passed into the function. This will make it so if the player is hit by something, you can tell it how much damage or healing to apply.

Then inside of this function we will make it so the healthValue is added to the currentHealth. Then we will need to add a limit to the max amount of current health the player can have, and what happens when the player reaches 0 health.

We do about the same thing for the enemy script as well, except just call EnemyDestroyed() function at the end.

Then I just went into Unity and made sure to set the player health and damage, and set health on the enemy as well.

I had to then readjust my Laser script so it would know how much damage to do.

I also had to get a reference from the player on the Laser script so in my FireLaser() function I needed to update that to give the fired laser a reference to the player.

--

--

Kenny Pruitt
Unity Coder Corner

Unity Game Developer, C# Developer, and Software Engineer