My journey becoming a Unity game developer: Enemy AI-Setup the guards to patrol the area (Part 6)
Objective: Setup the guards Idle and Walk animations to play when the guards are on patrol. Also, create eyes for the guards to detect and capture Darren moving through the room. Additionally, have the game over cutscene play when Darren is captured.
Let’s begin with modifying the waypoints that each guard will use in their path. The 1st guard will keep the same 3 waypoints in their positions. The 2nd guard will get an extra waypoint added to his path which will give 4 waypoints to navigate. The 2nd and 3rd waypoints will be positioned across from each other in front of the display case. The 3rd guard will have only 2 waypoints instead of 3, and the starting position will be next to the middle display case with the 2nd guard as well.
There are a couple of ways for the guards to detect the player. The first way we will use a Raycast to see Darren. Select the Player game object and in the Inspector create a new Layer in the 8th slot name Player. We will use this layer for the guards to focus on the player specifically.
Create a new script name Eyes that will be used to setup the Raycast to detect the player. Inside the Update() function, create a new variable with the Type Ray to be a new ray that will have a ray come from the guard’s current position, and have its Vector move upward to shoot the ray from the middle of the guard’s model. Last, make the ray aim in the forward direction of the guard.
Next, create another variable name layerMask which will look for the ray to detect the Player’s layer only. Create another variable to detect the object the ray hits. Last, create one more variable to shoot the rays in a forward direction by 100 meters.
Check IF the Raycast is coming from the guard’s position, shooting in a forward direction out to find an object’s collider at a certain distance only looking to hit the Player’s collider.
If this condition is met, draw a ray from the origin of the guard going forward, color the ray yellow and let’s keep it on the screen to see when it hits the player for 10 seconds. Also, use Debug.Log to show in the console how many times the guard detected the player.
Now when the guard detects Darren, we will see a yellow ray appear and stay onscreen for 10 seconds. But using a Raycast to detect the player isn’t as accurate as we would like. It’s better at hitting pin point locations like in a shooter game.
Using rays to see Darren is expensive as using Raycast in the Update() function calls for the rays every frame which can effect the performance of the game by lagging considerably. Plus, we would have to use something like SphereCast to have a wider radius for the ray to see Darren.
However, both Raycast and SphereCast are too expensive to guarantee the game will work without lagging issues. Therefore, let’s try something easier to setup for the guards to see Darren. We’re going to use game objects and triggers to detect Darren.
Create a Cube game object inside of the guard and set its Transform positions to zero. Scale it out to a distance you’re comfortable with to detect the player.
Turn off its Mesh Renderer so we will only see the collider itself. Duplicate the Eyes object 2 more times and place them inside the other 2 guards. Copy the first Eyes game object Transform Component Values, and Paste the Component Values into the other 2 Eyes game objects Transforms.
Select all 3 Eyes game objects, turn on the Is Trigger setting in their Box Collider components, and drag the Eyes script into the 3 Eyes objects.
Also, for the triggers to work we must add a Rigidbody to each Eyes game objects with the Use Gravity turned off as well.
We adjusted the Eyes game objects Transforms Scale and Position because the length of the objects were too long for our liking.
Clear out the Eyes script, and create a new variable Type Game Object which will use the game over cutscene we created earlier. Then using the OnTriggerEnter() method, check the condition IF the other Tag equals the Player we will use the game over scene variable and set active to True.
Play the game and we will see the game over cutscene play after Darren is detected.
The fade in effect for the Game Over Cutscene moved to fast, so we will open the Timeline and double-click on the Camera Fade Alpha animation track to open the Animation tab. Drag the 2nd set of keys to about the one second mark to allow the fade in to be seen.
Because we opened the Game Over Cutscene in Timeline, the Main Camera switched its position to the Game Over Cutscene’s position instead. Therefore, when we play the game, we will start from this position and not be able to see Darren.
To fix this problem, head into the LookAtPlayer script attached to the Main Camera. Create a new public Type Transform variable name startCamera for the starting position of the Main Camera at the beginning of the game. Inside the Start() function, set the Main Camera’s transform position and rotation to the startCamera position and rotation.
Click on the Main Camera game object, and drag the Camera One progression camera into the Start Camera slot in the LookAtPlayer script component to use this as the starting point in the game.
Next time we will start working on a Coin Distraction system for the player to distract the guards.