Day 67 — How to let the AI see

Connor Fullarton
3 min readMay 21, 2021

Hey and welcome!

I’ve got a pretty fun one for you today where we’re going to implement some functionality that will allow our guards to spot Darren if he sneaks by in front of them.

In order to do that we’re going to create a trigger that stretches out in front of our guard and if Darren passes through this then he’ll be spotted. Go ahead and create a cube object and make it a child object of one of the guards. Then stretch it out on one side to about 4.5 or a length that you’re happy with and then hide the mesh renderer and then check the Is Trigger box in the box collider.

Position your cube object so that it’s about chest height and comes out in front of the guard like this:

It may be a good idea to rename the object to Eyes so that we can grasp its purpose at a glance. Now add the rigidbody component to the object making sure to turn gravity off and create a C# script called Eyes with this code in it:

public GameObject gameOverCutscene;void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
gameOverCutscene.SetActive(true);
}
}

I bet you may have been expecting to see a lot more code here for this functionality? You should be able to recognise what all this code is doing but the short of it is that when an object with the tag of Player collides with Eyes it will set the status of our gameOverCutscene object to true.

If you’ve been following along you should have a Game Over cutscene that you can slot in as the game object the script is looking for.

Then when you run your game you should have something like this:

With that done you’ll have noticed that we’ve just successfully created our very first cutscene trigger!

If you run the game again though you’ll find that the camera is probably at a weird angle and that’s because of how we’re handling the behaviour when the Main Camera takes the position of a virtual camera.

Luckily it’s a pretty easy fix so let’s add in the following code to the LookAtPlayer script:

public Transform startCamera;void Start()
{
transform.position = startCamera.position;
transform.rotation = startCamera.rotation;
}

Then you just need to assign the Camera_One object from Camera_Progression_Angles as the Transform object that it looks for.

Now when you start the game it will always have the angle that you set for Camera_One regardless of where the Main Camera is before the game starts.

Pretty handy little fix we’ve made here and it helps having a designated starting camera anyway for a game.

That’s all for this article, next time we’ll be looking on giving Darren a new coin throwing ability!

--

--

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.