How to let your AI “See”

Discussing how I implemented a mechanic which allows enemy AI to “see” a player

Gabe Gomez
2 min readJun 29, 2021

Objective: Implement a means for our enemy AI to recognize our players.

How to let them See

Examples of Raycasts being sent out by GameObjects

In Unity, there are a few ways we can mimic the concept of “sight” for the AI we want to implement. One way we can do this is with Raycasting, and the other method we can use is to simply set up a collider and utilize Unity’s physics system to detect collisions. For this article, I will be doing the latter.

Implementing the Feature

To start, I created an empty GameObject and attached a Box Collider and RigidBody component to it.

I then make sure to place the transform and box collider in a position where it is in front of the guards.

With this setup, we can now detect the player when they are within that collider. The next thing to do is create a script to handle this. In my case, when a player is seen by the guard, the game over cutscene will commence. Below is the code I used to get this going:

As you can see, pretty easy and quick in this case. Below is the result of everything

That marks it for today’s article. Thanks for checking in! More to come soon.

--

--