Modular Health System
Objective: Create a health system for both the player and the enemies.
Create a new C# script named Health. This script has int variables for _maxHealth, _minHealth and _currentHealth.
Attach this script to both the Player and Enemy. Both _maxHealth and _minHealth are assigned in the inspector as shown below.
In addition, it has a public method Damage(), when called receives an int pain which reduces the _currentHealth. If the _currentHealth drops below the _minHealth the object is destroyed.
The Shoot script is modified to call the Damage method in the Health script if the ViewportPointToRay hits an object with a Health script. The example below passes the integer 10 to the Damage() method. Two float variables were also added to the Shoot script, _timer and _coolDown, to create a delay or cool down between shots.
This modular Health script can be added to either enemies or the player, and compartmentalizes the code managing health and damage.