Merging the Enemy functionality

Simplifying the Enemy AI script for better Optimization

Now that we have completed one enemy set with full animation with Attack and Damage functions, it is time to apply these features for the rest of the enemy types. Since we are already using Abstract classes it is best to incorporate as much class inheritance as we can to streamline scripts.

Checking for Script Similarities:

The very first signs that you will need to incorporate Abstract classes is when there are several scripts that are repeating itself under the same component Type. In this scenario we can see that the Skeleton and the Moss Giant are having several scripts that are overlapping each other such as the sprite flip since all enemies should be facing the player before attacking. Also the Damage method which will control when the enemy is going to play the Hit animation and toggle the In Combat animation bool is repeating itself.

The revised movement method for the Skeleton and Moss Giant after the Merging into the Parent Enemy class

Meanwhile in the parent Enemy script we will paste the facing player script into the movement method.

Placing the sprite flip within the Parent Enemy Movement method

For the Damage method, this will be a special scenario where we don’t have to incorporate the Damage method into the parent enemy class. This is because we will end up losing the ability to identify which enemy the player has collided with. By keeping the Damage Method separate we will be able to customize the individual settings per enemy type without using the override ability.

Remember to always do a double check before importing/merging scripts together just to see if they still work after the merge.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Simon Truong

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.