Super Easy Patrolling AI | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to a key Smosh Studios tutorial in this video I'm going to be showing you how to create your own basic patrolling AI through the use of waypoints these waypoints are going to be represented in the scene with game objects and then we will take those game objects and pull them into our scripts as transforms so that way we can have our AI translate between them as you're watching if you find this video helpful please remember to Like and subscribe the first thing we're gonna do is set up our scene I'm going to begin by putting in a plane and then I'm gonna have a sphere and the sphere will represent our AI and then I'm gonna create a cube I'm going to rename this waypoint and I'm going to give it a size of 0.1 I'm just going to move it over this way and then I'm going to duplicate it twice and move these around wherever as well from here I'm going to go ahead and create a script and I'm gonna name it patroller and go ahead and open it up the first thing we want to do is add our variables I'm gonna have two public variables the first one is going to be that transform array for our waypoints and then the second one is going to be an int and this is going to be speed and the reason I'm making this public instead of private is so that way you can use the same script but have AI act differently so you can attach this on to an AI that you want walking between waypoints as well as an AI that you want running between waypoints and you can change the speed of them inside the inspector instead of having to create different scripts for your different speeds we're gonna have two private variables as well the first one is going to be our waypoint index and this is how we're going to keep track of which way point we're on within the array and then the next one is going to be a float and we're gonna use this float to keep track of the distance between the AI and the current Waypoint from there we can go ahead and initialize things inside of start so we're gonna go ahead and put our Waypoint index to zero as we want this to start on the first Waypoint in our array and then we also want our transform to be looking at that Waypoint so I'm going to transform look at the waypoints Waypoint index dot position and this just has it toward the AI is facing towards the Waypoint so that way when we make the movement go forward on the AI it will be moving in the direction of the Waypoint and then we're gonna go ahead and skip update and create two functions the first one is going to be patrol and this is going to just be one line of code and it's going to be our movement so I'm gonna do transform translate and then we're gonna do vector three dot forward then this gets the z-axis of the AI and then we're gonna multiply it by our speed and then we'll multiply it by time.deltatime our next function is going to be for incrementing our index so we can go ahead and do void and I'm going to name it increase index and then inside this function what we want to do is add to our Waypoint index and then we want to see if our waypoints index has gone out of range so we'll go ahead and do waypoints index greater than or equal to our waypoints dot length and this is checking if our Waypoint index is greater than or equal to the array of our waypoints if it is out of that range we want to make sure to reset it back to zero and then from there the last thing to do inside this function is make sure that the transform is now looking at the new Waypoint so now that we've done all that we can go back up to our update function and go ahead and fill in what we're gonna put here and the first thing we want to do is go ahead and check the distance between our AI and its current Waypoint and the way we're gonna do that is by using vector 3 dot distance and then we do transform dot position and then we're going to do waypoints Waypoint index dot position and now we want to check to see if that distance is within range of our Waypoint I'm going to make that range of value of one but you can have that range be whatever is most comfortable for your game this is simply just saying is the position of our AI within one unit of our Waypoint and if so we're gonna go ahead and increase our index and then each frame we also want to make sure that we're patrolling so that way our AI is moving and that's the entirety of the script so we can go ahead and go back to the scene and click on our sphere and then inside of this we want to go ahead and add our script we want our Waypoint size to be three and then we can go ahead and drag all of our waypoints in and the way your AI is going to traverse these is from element 0 to element 1 element two I'm going to go ahead and give my player a speed of two and again the speed can be whatever you feel is best for your game and before we test and play I'm going to adjust the camera so we can better see the field and I'm also going to add a material so that way everything isn't completely white so now we can go ahead and play and as you can see the spheres going between the different way points and it will do this indefinitely so as a recap we put game objects into the scene to show where the AI would be patrolling and then we pulled the transforms of those objects into an array in our script so that way we could iterate the AI between their positions as always I hope this video was helpful if you have any questions or suggestions feel free to leave them in the comments or you can join our discord and ask them there the link for that will be in the description below don't forget to Like and subscribe thanks for watching see you next time
Info
Channel: KeySmash Studios
Views: 11,096
Rating: undefined out of 5
Keywords: Make my game better, how to, unity, learning, guide, easy, Unity basics, learning to unity, unity starting, how to start unity, my first unity game, unity tutorial, Unity scripts, Game design, unity engine, c#, c sharp, step by step, AI, artificial intelligence, enemy, enemies, friendly, helpful, waypoints, way points, traverse, iterate, patrol, patrolling, patroller, movement
Id: 22PZJlpDkPE
Channel Id: undefined
Length: 7min 26sec (446 seconds)
Published: Sat Mar 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.