Day 77: How to let your AI See!
Objective: Allow the guards to catch the player and start the Game Over Cut scene.
Time to allow our guards to catch the player in the act!
To start we need to give the guards a collider that our script will attach to. On a guard, right click -> 3D Object -> Cube
The cube should spawn as a child to the guard and be at his feet.
Next will to move the cube up to about under the guards chin and resize it to where you think the guard would be able to see.
Next will to uncheck the MeshRenderer so that the cube will disappear and you only see the Collider area.
On the Box Collider, we will need to check Is Trigger. This will allow our player to enter the area and active the trigger.
Everything is set up to be able to catch the player but we have no logic. Time to set up the logic to catch the player.
We need to create a script called Eyes and attach it to the cube. We can also rename the cube to Eyes.
Inside our script we can remove update and start functions, for this script we don’t need these as of right now. We will use a OnTriggerEnter function and compare the collision to the tag of Player. If it is the player we will enable a cut scene. To enable a cut scene, we need a reference to that object. We get a variable of type GameObject and name it _cutscene.
Now to get the trigger to work 100% correct, either the player or the object with the Eyes script will need to have a rigidbody. For this game I added the rigidbody onto the Object with the eyes script.
And that’s all that is needed to catch our player in the act! Try to stay hidden or you might get caught now!
Thank you for reading and have a wonderful day!