New Feature — Aggressive Enemy
Objective: Implement an enemy type that moves towards the player’s last known position when in range
So far this game has enemies that all shoot projectiles. We want an enemy that will challenge the player to move by moving towards them. Let’s create a new Rammer script that inherits from the EnemyBase class and uses the move speed property.
With this code, the Rammer will move down the screen.
Next, set up a property that will store the range for detecting the player. We will use this to start the ramming behaviour.
While we are at it, let’s display this range in the Editor whenever the Rammer is selected. This really helps visualise the range so you may tweak it while the game is playing.
When the player is within range, let’s set a flag that we are ramming and head to the player position. We have this flag so we do not constantly update the player position. We are only interested in the first instance. Let’s encapsulate this in a helper method and call it from the Update method before the Movement method call.
Let’s also change its behaviour when the isRamming flag is true.
Ok, so we now have it going to the last know position. What next? we just can’t have it stay there. Let us have it continue its path downwards when it “arrives” at the position. We need another flag to signal this arrival and factor that into the Movement method.
Finally, we will override the OnTriggerEnter2D method with how we want a Rammer to affect the Player on contact and what to do with the Rammer.
That’s it, create a new wave, add as many of these Rammers you like, and tweak the gameplay till you have it just as difficult as you want it to be.
If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.