One of the efficient ways to create a game is creating modular system.We had created abstract class and interface, which are all kind of modular system. In this article, we would create a simpler modular system as health system.
This health system would be built as a new script.
Create a new script and create a public method called “Damage”.
And we also need some variables to set the max and min health value.
This public method “Damage” would be the core of this script.
Next, we will need to make this method calculate the current health when taking damage. Thus, we need to pass in the damage amount when it was called by other scripts.
The Health script is ready, now we can put this to both Player and Enemy.
Damage on Enemy
To damage the enemy, we can simply use the crosshair with mouse clicking.
Open Shoot script and add few lines to execute shoot.
With these codes, when aiming on Enemy and clicking mouse, it should locate the Health script. If it has, then deal it with 5 damage.
Now back to editor and assign the max and min health to Enemy.
And we can test out the damage result.
The next article we would continue making damage to Player from Enemy.