Making “Eyes” in Unity(Game Dev Day 61)

Jack Leavey
2 min readJan 16, 2022

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.

The green outline shows the guards’ “vision”

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.

Make sure gravity is OFF.

Inside the Eyes script, I added the following code to check for the Player when they enter the box’s area.

This script checks for a collision with an object tagged “Player”, then plays the Game Over Cutscene if It does have a collision.

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!

As the guard turns and reveals the Player, the game over cutscene begins!

--

--

Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!