How to let your AI See!

Simon Leen
2 min readAug 6, 2021

--

Objective: Let the security guards see the Player.

We have a moving, animated Player and Security Guards but we need the guards to be able to trigger when the Player is in line of sight. There are multiple ways to do this but for this project we will use a trigger activated when the Player passes through the guards Eyes GameObject.

For the eyes we will set up an Cube GameObject within the model, and make it a specific size to act as line of sight. We will position it starting at the front side of the guards body.

We can then turn off the Mesh Renderer and set the Box Collider to trigger.

Do the same on each of the guards and we now have moving line of sight on each guard.

To allow the Eyes to see the Player, we create a script called Eyes and attach it to the Eyes GameObject on each guard. We need to set an OnTriggerEnter that triggers when the Player passes through the line of sight(the cubes). In order for the trigger to work, at least of the GameObjects in collision needs to have a Rigidbody attached so we can add one the each of the Eyes but make sure to turn off Use Gravity.

Inside the script, we need to create a public GameObject for the previously created Game Over Cutscene. We will trigger this when the Player triggers any of the guards eyes. We will assign the cutscene in the Inspector.

Video is a little choppy due to recording while rendering in Unity.

--

--

No responses yet