- Home /
Universal Damage Sytem
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.
Vars Needed
private float fighterHealth = 100; private float DropShipHealth = 400; private float Player = 200; private float motherShip = 4000;
Functions needed
public void DetectShipType(){ // find out what ship type this is
}`
Answer by Nemox · Nov 01, 2012 at 11:41 PM
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.
Thanks, yeah I somewhat remember Inheritance from my C# learning. But don't really know how it applies yet in Unity since scripts go on gameObjects.
Scripts that go on GameObjects are ones that inherit from the $$anonymous$$onoBehaviour class. In Javascript/Unityscript all scripts automatically inherit from it.
Just the same if a Ship inherits from $$anonymous$$onoBehaviour, anything that inherits from Ship also inherits from $$anonymous$$onoBehaviour. That means it will have all the same variables and fuctions that Ship has, and also what $$anonymous$$onoBehaviour has.
One other quick note, if you ever see "extends" in Javascript, it's the same thing.
Answer by asafsitner · Nov 02, 2012 at 08:04 AM
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.
Your answer
Follow this Question
Related Questions
how do i make a script that calculate how much damage did i loss 1 Answer
For loop on collision (two hit kill) 2 Answers
Falling Damage 1 Answer
Damage not triggering in build 1 Answer
FireBall script 1 Answer