my problem is that I can’t figure out how to properly structure & code in Unity. I’m working for a University assignment, specifically spoken an augmented reality tower defense game.
My Case: I’m having defensive items to place which are walls and towers. Then i have enemies which try to kill these.
All three will get a HealthBar, which is an empty gameObject with an added health script and has as child a canvas showing the actual left life as a bar upon the object.
So i’ve gone the way that it is a standalone prefab like the tower, wall and enemy. The meshes for all three are just primitives at the moment, which i will replace at a later stage.
For this reason i thought it’s best if i add the HealthBar dynamically in code.
I’ve got a GameEntity class which has this as a attribut and the classes tower, wall and enemy derive from this base class.
I’ve not got it working for now, that the healthBar is shown in the scene - i don’t know why at the moment but that’s not part of this question.
I just wanted to ask in the meanwhile if it’s the right way to do structuring in Unity.
Later on i need to add shooting for towers and enemies - should i make an interface class for this which gets implemented by both and simply has an attack method which both override ?
The “Unity” way to do this is to implement this through several components, rather than through inheritance.
Instead of creating a Tower class that inherits from GameEntity, you have a Health class and a Turret class, and you create a tower by attaching both of those to the same GameObject. Assuming that the Towers even have health.
Enemies consist of Movement and Health, and walls are just Health. Then things that attack look for Health (Or Damageable or whatever abstraction floats your boat) instead of GameEntities. The health bar’s script will simply have a Health componenet attached that it cares about.
The idea of this design is that it’s faster to create new things - if you already have a HP script for enemies and towers, then walls are pretty much already done. It’s also really hard to share many kinds of behaviour when you don’t have multiple inheritance.
Thanks for this answer!
Just to clarify: I’ve then a prefab turret (which basically is the mesh) and a prefab HP. Do i have then an extra script turret where i create an empty gameobject and add what is needed like the turret & HP. And then this gets instantiated when it’s needed in the game ?
Can you please explain this in a few more lines, i didn’t understood the message.
class Actor
{
Position position;
}
class LivingActor : Actor
{
Health health;
}
class Player : LivingActor
{
Equipment equipment;
}
We’d do:
class Player
{
Position position;
Health health;
Equipment equipment;
}
So your enemies, walls and towers would all have their own Health component:
class Health : MonoBehaviour
{
public int health;
public void TakeDamage(int damage)
{
health -= damage;
if(health <= 0)
Destroy(gameObject);
}
}
As for how to actually draw all the health bars for your GameObjects, there are a number of of ways to go about it. A simple way would be to have the Health behaviour register itself with a HealthUiManager during OnEnable and OnDisable. The HealthUiManager would be responsible for managing / updating individual health bars.
A Health Bar prefab would definitely be separate from your Wall, Tower and Enemy prefabs as it requires different components (UI related one).
That depends on what your need are. You might have a prefab that collects all of this together (the mesh, the HP script, the shooting script, the hp bar canvas + script). You may also have a simple prefab with a helper script that instantiates the things you need.
Usually the prefab is easier, so I’d go for that, but it’s not neccessary, if you find it more comfortable to change things through changing scripts. If you were working with designers and artists and other non-programmers, you’d want to keep details on the prefabs so it’s simpler for them to customize the game (ie. so they don’t poke your shoulder every 15 minutes)
Say you’ve got a health behaviour, a shooting behaviour, and a movement behaviour, and want to make things that:
Shoot, have health, don’t move (Towers)
Shoot, have health, move (shooting creeps)
Shoot, don’t have health, move (a temporary powerup that runs accross the field and shoots stuff)
Don’t shoot, have health, don’t move (walls)
Don’t shoot, don’t have health, move (idk, birds in the background?)
etc.
How do you solve that with inheritance? Just marking things with interfaces doesn’t really help, you still need the behaviour implemented somewhere. If you were writing c++, you have multiple class inheritance, so it would be possible (though probably ill-adviced) to have a shooter class and a mover class and a health class and have your stuff inherit from those. In C#, you can’t, so the component design where you combine behaviours by attaching several objects together makes the most sense.-