Day 37: Unity 2D Game Development: Enemy Upgrade — Aggressive Enemy

Goal: Create an enemy that will chase the player and ram into them

Josh P (Pixel Grim)
2 min readNov 14, 2022

Time to add another enemy type! This enemy will translate down the screen as usual, but if you are in its field of view, it will target you and try to ram you. If you are faster enough to get out of its way, it will return to normal behavior and forget about you.

Battering Ram Pre-Fab

The first step is to create another enemy pre-fab called Battering_Ram_Enemy this game object will have the enemy script, a unique sprite, and a box collider.

Enemy Script

A few adjustments are needed to be made for the enemy script. Currently, enemies are assigned a random movement ID in the start method. This movement ID is then used in a switch statement assigning the instantiated enemy a move-set(side to side, Spin in Circle) but for my aggro enemy, I want him to have the same movement ID every instantiation. In order to do this the start method looks for the battering ram prefab name and then assigned the movement ID to 3.

For the Ram Attack movement, we calculate the distance from the enemy pre-fab to the player. Then if the value of the distance is less than 3 we want to translate towards the player “ramming” them. If we break out of the enemy lines of sight then translate down the screen as they normally do. Viola, we have an enemy that will chase after the player.

===============================================================

--

--