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.

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Get ahead using headers in RecyclerView

2 illustrated phones. One without header and one with header.

Android Fragment Test with Espresso + Mockk + Kotlin

A smartphone sitting on top of an opened laptop.

Error hierarchies and Kotlin sealed interfaces

Postmortem: How I got banned from /r/androiddev for being “too” active as a member of the…

Android Kotlin Get WiFi IP

⚡ Is Your Android Studio Always Slow? 🚀Here’s How to Speed Up Immediately.

Tap, Dash, and Avoid Colliding in Target Dash

Paging 3 — Easier Way to Pagination

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
Jean-Noel Seneque

Jean-Noel Seneque

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity

More from Medium

Using Tessellation in Unity’s HDRP

Application.Quit — Exiting the Application

Aiming Features in Cinemachine

2.5D Platformer — #13 Rolling