Enemies design with abstract classes
Let’s say we have these enemies:
As we can see, all of them share the same variables, so we can create a class from which they can inherit those values
It seems to work fine so far, but there is a little drawback: all the enemies will execute Attack()
in the same way. Let’s say we’ll call that method in Start()
in both enemies:
And obviously we want each enemy to behave differently. For this, we can use abstract classes and methods. Further information about these topic can be found on internet, here we will use a simple implementation.
Keep in mind about abstract method:
- It forces the child classes to implement it
- Requires the abstract keyword
- Can be declared only in an abstract class
That way each enemy will implement the Attack()
method as desired, and it will be executed independently
Now, we will call Attack()
in Start()
in Enemy
script and we will implement the first one in each enemy. It will be executed in the whole scripts that inherit from Enemy
, even if they don’t call Attack()
in their own Start()
method