The next part of making our zombie fps game is creating a health system. We want it to be generic so we can use it on any entity that needs it.
let’s begin by making a Health script and placing it on both the Player and Enemy objects.
So what do we need to know when dealing with health? Generally we’ll need a maximum amount of health or hp, a minimum amount which if it goes under, the object will ‘die’, and the current status. So let’s create some variables in our Health script:
Since we used the SerializeField, we can now see our variables in the Unity inspector:
Let’s give both the Player and enemy max 100hp and 1 minimum hp.
Next, we were given the challenge to implement a Universal damage system: We’ll need to subtract damage from our current health and also check if we went below our minimum so let’s get to work:
Let’s create a method called Damage(int damage). The smaller damage will be a parameter that’s passed into it.
Remember that it will be someone other than the script holder that will be doing the damage, so we’ll need to make the method public. It will pass a parameter that gets placed in the damageAmount variable, which is then subtracted from _currentHP. Easy enough. Now let’s check if enough damage was had to ‘kill’ our object.
Also easy! Just check if our current hit points is less than the minimum, if it is, then we’ll destroy the gameObject.
Congratulations! We’ll be using our newly minted health system by using it to kill our enemies tomorrow, and also some virtual cubic zombies!