Making “Eyes” in Unity(Game Dev Day 61)
With the guards moving around now, it’s time to give them some gameplay functionality.
There were two ways I could go about creating a “vision” system for the patrolling guards. On one hand, I could use Raycasting, which could be used to give a “realistic” sight to each guard. However, for this game I decided to use some box colliders to detect collisions with the player, as the guards need some limitations in order to facilitate gameplay.
First off, I made an “Eyes” gameobject that was simply a 3D cube with the mesh renderer turned off.
I attached one of these to each guard as a child object.
Next, I attached a new Script (Eyes) to these 3 gameobjects, as well as a box collider.
Inside the Eyes script, I added the following code to check for the Player when they enter the box’s area.
I also added the Game Over Cutscene Gameobject into the Inspector window of the Eyes Gameobject.
So now when a player enters the line of sight (AKA invisible box in front of the guards), it will play the Game Over Cutscene!