Hello, welcome to this lesson in Unity. In these lessons, we will add interactions and code gameplay from both the player and NPC in Unity. Open the NPC scene. To review, press play. Our NPC walks towards the waypoints and pauses when there. Stop play. As you may have already guessed, we are going to need more waypoints in the arena so the NPC can patrol around and search for the player. To simulate the NPC appearing to look around, we should randomize which waypoint to go to after the first one is reached. That way, there won't be a pattern, making it too easy for the player to tag the NPC. Open the NPC script again. We already have a waypoint variable, but we want to access all the waypoints added in the scene and be flexible, if the designer wants to add less or more. To do this, we will change the transform type to an array by adding brackets after the word transform. An array can be a single value with an index of 0 or larger. For example, an array of 5, set in the Unity editor, will start with index 0 as the first element and end with index 4. The array length will then be 5. Select one item in an array, you need an index variable which has to be an integer. Make room for another variable and type private. This be an integer, and we'll name this variable WPIndex. And we'll make the index, we'll start it at 0. This variable will work in conjunction with the waypoint array. Notice the red squiggle under the word position in the update method. This means there's an error in the code since we change the transform to an array. We no longer have a single waypoint, so change waypoint to waypoint bracket and inside the bracket, we want to add WPIndex, which is our integer. Notice the red squiggle has gone away, so our code is good. You can also control the NavMesh Agent speed, which will help when we need to stop the NPC with the speed of 0. So make room for another variable and type a public. This will be a float. We'll name this NPCSpeed. And we'll set it to the value, the NavMesh Agent is normally set to which is 3.5. Don't forget the equal sign. We made this value public, so we can change the speed during testing. That's also set a variable, control how far the NPC can be from waypoint, before adding to another waypoint. This will also be public. Another float. We'll name this one, how close to target. We will set this to a low number like 0.25. And we use the letter F to designate it as a floating value. We made this variable public, so when finalizing the game functionality, you can adjust the level of difficulty by adjusting these variables. We will start making use of custom functions to keep track of our code, by breaking out parts of code that will be repeated. At the last curly bracket of the update method, click, press enter twice and make some space. Now we're going to create a new function of our own, named Patrol. Remember a function should be properly formatted, starting with a capital letter and any of the parentheses. The start and update methods can fill up with lines of code very quickly, which may get confusing. Select the line of code in the update method, cut, and paste it into the Patrol. Now call out the Patrol function from update. Don't forget the parentheses. Now you can see how neat or this code is looking. This can make it much easier to troubleshoot as we add more code. Select the code, return to the editor, select NPC in the hierarchy, note and inspect it that waypoint has changed, and now you can type a number for the expected number of waypoints you would use in the scene. We have five waypoints, we'll type in five, now if we twirl open the triangle, you can see we have five slots for all waypoints. Select waypoint in the hierarchy and reset its transform, that puts it in dead center. Zoom out of the whole arena, click the green Y cone, now give us a direct overhead look, select waypoint, duplicate it, and using the square, drag it into the corner but not too close to the corner, give it a little bit of room, duplicate again, drag it to the next corner and do two more times, recover all four corners in the middle, now we should have a total of five waypoints. Select NPC and the inspector drag it in each waypoint into a slot, the order does not matter, we'll be randomizing the patrol in code, press play, you turn on gizmos, the NPC is walking in place, that's because the first waypoint is in the same spot as the NPC, open the scene tab, move the NPC away from the waypoint, it will move back to it, stop play, the NPC is not going to the other waypoints, that's because we need to edit the script to determine if the NPC is near a waypoint, then randomly pick a new waypoint to go to, this will simulate a random patrol, open the NPC script, now add this code at the beginning of the patrol function to change the waypoint if the NPC is close, and this will be an F statement, what this code is basically saying is, if the NPC is close to the latest waypoint, which we set to 0.25 units away, then change the waypoint using a random integer between 0 and the number waypoints added, in our case 5, add this code at the end of the patrol function, this just sets the agent speed, and we're going to set this to the NPC speed variable we made, save the script, return to unity editor, select NPC in hierarchy, let's press play and see what happens, turn on gizmos with the not on already, you should see all the waypoint icons in the NPC walking to each, open the scene tab to get an overhead view, turn on the gizmos with the not on already, you should see all the waypoint icons in the NPC walking to each, set the NPC speed in the inspector to 15, note at times the NPC may circle back to the same waypoint, that's okay and is part of the random patrol, now we'll set how close to target to 20, now the NPC is really running around in a random pattern making it easier to tag the player, so you can adjust it for an easy or to a challenging gameplay, now set the NPC speed to 0, the NPC slows to a stop, we will use this later in code to freeze the NPC when tagged, stop play, congratulations, you have now created a random patrol for the NPC, save the scene, in the next lesson we are going to code NPC line of sight detection in Unity, thanks for watching!