Making An Enemy AI That Can Detect When The Player Is Close

It seems that most forms of enemy AI these days are developed with some form of detecting the player. Some use sounds, while others use sight, etc.

But how is this done?

The simplest answer is: “Colliders”.

If the player steps over an invisible tripwire around the enemy, then the enemy becomes alerted to their presence. This is often referred to as the enemy's “Cone of Vision” because a lot of developers utilize a “Cone” type shape for their colliders that can detect the player.

The more advanced the AI, the more parameters and collisions go into this feature. From peripheral vision to partially obscured lines of sight defined using Raycasting, these detection systems can become quite extensive.

In my 2.5D Space Shooter game, I added a new enemy type that moves quicker than the others, and periodically flies down to the bottom of the screen to try and shoot the player from behind. But I added the parameter in there for the enemy ship to detect when they are behind the player and shoot.

To do this I added a collider with a longer range extending out in front of the enemy ship. This collider is attached to the parent object of my enemy prefab since It’s bigger than the collider I use to detect when the enemy takes damage. Once all the references were made, I added sets of randomized variables to dictate less predictable behavior, and bam!…

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

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