Hi, I don’t really want to make a health script for each unit in my game. Rather I’d like to create one script which holds all the health for each unit, when they lose health it goes to that script and they eventually die. But I don’t really have a clue on how to set this up to where each unit is unique when compared to this script. Can someone give me some hints on how to go about setting this up? So far I have 4 units including the player. Thanks. This is my pseudo code so far.
`Damage_System Pseudo - if it is a enemy fighter damage him based on weapons, same with dropship player and mothership.
Fighter will have 100 health
DropShip will have 400 Health
Player will Have 200 Health
Mothership will have 4000 health.
Start by learning a bit about Object Oriented programming, and how to use classes to your advantage.
You could have a class called Ship, and it has a float variable called Health.
If you make a Fighter class that inherits from Ship, Fighter will also have that same variable named Health!
You could make a DropShip class that inherits from Ship, and it will also have Health just like any other type of Ship.
Perhaps you have a special type of Fighter that’s like other fighters but also has special things that are different from all other Fighters? Maybe an XWing that inherits from Fighter and has a ProtonTorpedoes variable or something, while a TieFighter that also inherits from Fighter might have a SolarEnergy variable.
Consider BurgZergArcade’s tutorials. They’re for a Hack-and-Slash game, but the scripting fundamentals you’ll learn from it are very valuable.
In Unity, classes are behaviours. Additional classes add additional behaviours to objects.
If you want a generic damage system, just create a small class that handles damage; something like DamageTaker with a float variable for Health and two methods; TakeDamage and Die, where TakeDamage is called from damage-dealing sources, and Die is called when you want to actually kill the object, either from TakeDamage when it sees the object’s health drops below 0, or when you want to insta-kill the object.
Add this component to whatever you want to make it kill-able.
Alternatively, you can turn that class into an interface and have all your other classes inherit and implement it to make them kill-able.