Working With Inheritance For Enemy Setup

Niraj Karki
2 min readSep 18, 2021

--

2D Mobile Game

Objective: Create a enemy class where the enemies can inherit the most common variables and methods.

First of all create an Enemy script and make it abstract class so that we can define abstract methods to make sure the methods gets implemented in all inheriting class.

Here, the most common values are enemy health, speed and gems it drops after defeat which are present all enemy types. We declare two transform variables which will store the location between which enemy will move. We declare the Update as abstract method so that all the enemies script will implement update method.

Then unlike other scripts which inherits from the MonoBehaviour class, the moss giant enemy will inherit from Enemy class we just created and it will get access to all the variables and methods and make then unique to their own even if other enemies inherits and make use of them.

Inside the Moss giant script, create an override Update method since it has been declared as abstract method in Enemy class. Then inside the update function, check the enemy position where it checks if it is in either point A or point B and change the target to the other position. Then we used that target position to move the moss giant from the current location to that target position.

And this is the result.

--

--

No responses yet