How to let your AI ‘see’.

Right now, our guards can only walk around obliviously with no way of seeing Darren. Let’s give them some eyes!

Normally, Raycasting would do the job but there’s a much simpler method by using trusty boxColliders.

First, select the security guard, then create a cube so it’s part of them, extend it out a bit as this will be his line of site, turn off the mesh renderer, and rename the cube to “eyes”, and you’re halfway done!

Don’t forget to tick ‘is trigger’ for the box collider.

Next we just need to create and add a script to this new eyes object. I called mine ‘GuardSee’ though you can call it eyes as well to keep things consistent.

The script itself couldn’t be more simple. We create a holder for our game over cutscene, then in the OnTriggerEnter, we check to see if the guard ‘sees’ the player, and the cutscene is activated!

Let’s see it in action!

Oh no! It works! Well, in this case it’s a good thing!

Tomorrow I’ll update you on more cool Unity shenanigans!

cartoonist, game artist, and wanabe gamedev.