Learn AI in Unity Part 3 - Behavior Trees

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right so today in our third uh chapter of learning about artificial intelligence and gaming through unity we're gonna learn about behavior trees and we're gonna use a software package or unity asset called behavior bricks it's a really simple tool for for dragging and dropping ai behaviors it takes a lot of the heavy lifting out of your hands and it it it does all the work for you so we're going to do today is we're going to take uh an existing like a little maze here and this is all comprised of cylinders and we're going to have our little cube here chase our our sphere so it's just going to take a second to load all right and as you can see the cube is moving around and the sphere is it's chasing down the sphere the sphere changes position every one and a half seconds or two seconds or so so you're going to see the the sphere change there you go it's popping somewhere else and it's going to move toward the key is going to move towards it now notice a couple of things one is going around the walls okay that's the first thing the second thing i notice is that the behavior of the cube if you look exactly if you look if you stare at it for a bit it's actually wandering so there is actually a wandering feature built in to behavior bricks so it actually does the wandering that we talked about weeks ago when we tried to implement our own wandering behavior so they've taken care of that inside behavior bricks for a more natural movement so this is our goal for today so we're going to do is we're going to start with a project that basically has the cube the sphere and and the the walls and we're going to work with that so what i'm going to do is i'm going to take away this project all right so our first step right now is to actually install behavior bricks now behavior bricks also requires one other thing it requires nav mesh interestingly enough so last video we talked about how to install and use nav mesh we have to bring it back into our project so if you need to understand where nav mesh is please refer to the last video in order to down download and install nav mesh now uh seeing if you're following properly all you have to do is bring in if you recall from last video uh one folder called nav mesh components and so and so all you do is copy and paste that into your project so what i'm going to do is i'm going to bring that in right now so what i'll do is i'll just take something in my scene just go show explorer and got it right here so this is this is inside explorer from my current project i'm just going to take a navmesh components from another project copy and paste it here all right and there it is i'm just going to close this it's going to be thinking while it's thinking i'm going to bring up the the page the asset store page for behavior bricks so this is behavior bricks so it's in the asset store you can search for it this is what we're going to use today and i've already downloaded it but go ahead and download and install it and then hit open in unity and we're going to use the package manager to bring it into our project so so this is what it looks like in case you're searching for you need to download it and bring it in all right so now once it's done bringing in nav mesh we're going to switch over to the package manager and work on bringing this thing in now all right so now that it's done uh bringing in navmesh as you can see nav mesh is ready to go there we're gonna go to our window and our package manager and uh i already have behavior bricks right here and i can see the behavior bricks asset store definition import here information here now the trick is to download and import it i've already downloaded it in a previous project so i'm just going to hit the import button and let it do its thing there it is decompressing and we'll just fast forward a bit because it will take a bit of time all right so i'm just gonna hit import and let it do its thing it'll take a bit of time to download and install so let's uh let's fast forward i'm going to pause the video recording just to let it fast forward and you're not sitting through this entire thing but as a reminder you have to of course download and install it i'm just jumping straight to import because it's already i already downloaded it in the past you just have to quote unquote purchase it through the asset store even though it's free and once you've all done all that we'll reconvene after all right it looks like it's done so i'm going to close up the package manager window and we want to do first is we want to take our scene here and we're going to start off with with doing our nav mesh stuff first which is really simple we're just going to bake our scene here but to do that first we have to go to navigation and bake sorry and uh object click on yep so click on we have to click on all of our walls here so they're all cylinders they're all under again under the game object walls so i'm gonna click control click multi multi-select all of my cylinder objects all right and in the last tutorial we use the terrain this time you see a simple plane so everything there is going to be checked off as navigation static all right and making sure it says walkable all right so now i want to bake this so i'm going to click on the bake button here and i'm going to hit bake and it's going to take about an hour and a half to two hours in this implementation and based on my computer that i'm using right now take about an hour half to two hours depends on your computer depends on what you have uh set up for your implementation uh even if you have maybe more heavier duty example it might take longer than that for all we know but what it's doing in bake just to remind you is that nav mesh is calculating all possible paths in this little maze that i've created here so what you may want to do now is go grab a coffee grab some lunch whatever and pause the video and wait till your baking is done come back and then we'll continue on all right so that baking process took about six or seven hours in my case uh as opposed to the one to two hours i was just telling you about a moment ago i guess one thing to note when you're baking something you in navmesh close as many processes as possible i had things that i needed running such as the screen capture program to record this lesson but you know if you can close as many processes as possible close your browsers close anything that you don't need so you can speed up the process that's really what it's all about so anyway suffice to say we finally have our area baked all our paths are created you can see here the blue paths that we're talking about here so that's the beauty of it it's gonna it's gonna navigate around these areas and you see the graph that's uh that sorry the the the tree that's already built in so you can see that these are the paths that it'll take so uh we have here this script called npc mover and basically it's the same script that we we created from the last project the idea is that it's going to randomly place a target an object so npc target in this case in different spots uh uh based on uh well based on the range here so we do is we actually create a random dot range for the x between negative seven and five and random dot range for negative 19 and four for the z and we're keeping y at one always we keep hardcoding y equals one for height uh these are numbers i measured out physically by going to the inspector and seeing what they were so i actually where my mouse my mouse pointer is right now i actually physically measured these out so whatever maze you have you want to double check that your numbers match accordingly here and the idea here is that we're going to move our target position to to this new randomly generated location every one every one second or every sorry every five seconds so in this case we're using a method called invoke repeating calling method called next waypoint which is actually this and then we're just basically gonna repeat it every five seconds so it's gonna start after one second and it'll be repeating every five seconds so we already have this it's already attached to our cube it's already connected up to our cube and we have our sphere which is the target that's gonna that's already dragged into it so now that we have this up and running uh what we need to do now is set up our behaviors so now we're going to move over to behavior bricks and to do that i'm going to open up window and you see here behavior bricks editor so you've assuming you've already installed it everything's up and running nicely uh but this is the editor window that pops up and usually you would you would attach it to some place uh but it's it's a pretty big uh it's a pretty big window and you need all that space so i'm going to leave it like this i'll be closing and opening it over and over again in this case so you'll notice that it actually has two areas so this is sort of our inspector area and this is our our editor area essentially and you have here four buttons here so collection so you can create separate collections of behaviors or separate trees essentially uh for for the behaviors you want to do all right and then there's node which actually when you click on a node that we're going to have here it's going to you can actually edit it so it's like the editor portion of that node behavior what behavior is it attached to right what collection is it attached to and blackboard basically handles variables so you can have variables that can interact with the different behaviors and so you would see those variables that sit out in blackboard so our first thing to do in collection is to create something and create a new behavior all right and it's gonna ask us for a location so uh i'm going to put it i shall leave it here i'm just going to call it wander right it's going to wander around let's call it wander and hit save and you notice that it's generated this wander right here and it's changed the editor area to actually be like a grid like structure so we have something to work with so now let's add a new behavior so i'm going to whenever you want to add a new behavior brick to your editor just right click and you can search for what you're looking for so i'm going to search for move and i want to move to a particular game object i want my cube to move to my sphere so i'm going to click on move to game object and there we have moved to game object and now you'll see that if i click on node there are so there are now settings here i can work with so i want to set it up so it moves to my sphere and you'll notice that by default it's expecting to enter some constant value i don't want to hard code my value i don't know what the value is going to be so i want it to be based on another game object so i'm going to change constant to blackboard all right to tell it that there's a variable i'm going to work with and i'm going to i'm going to give it a proper name instead of just target let's call it wander target all right you can call it sphere target whatever you want but give it a proper name hit create now we have a variable all right so now it's going to based on the variable data it's going to move to that location all right now uh for now you'll notice that this is all we're going to do right now right we have a simple behavior tree of one object right which is good enough to get started so i'm going to close this and i want to bring this up bring this operational so what i'm going to do is i'm going to go to my hierarchy click on cube and i'm going to add a new component below npc mover script and this new component is called behavior executor component right behavior executor component right this will actually bring uh bring behavior bricks on uh behavior breaks his uh meth well behaviors onto this particular cube so the first thing to do is specify the behavior well we just created a behavior called wander right we we created a behavior called wander so let's click on the dot and i'm going to click on wander right so this is when we created all right now you'll notice that there's something called behavior params because we created a a blackboard object called uh blackboard variable called wander target we have to specify what that is now and our black our wonder target will be our sphere all right and now we can go ahead and click run and see what happens all right now we're running it but there's a problem and you'll notice that one the sphere is all over the place right you'll it's uh it's on it's it's uh all over it actually is is ignoring the the walls that we have created and the cube isn't really going anywhere all right and the problem is because uh we didn't add one important thing which is to specify that the sphere is a nav mesh agent so let's stop the playback here we're going to go to our sphere and we're going to add the component nav mesh agent there so i have navmesh agent and i'm going to leave the default settings as is i don't really need to worry about those default settings so so this is good enough now let's give it a try again and see if this is gonna work now all right and there you have it you see here that the i'm using the scene view to illustrate but uh let me click away to something else there we go now you can see a bit better the sphere moves the cube follows and you notice it's it's applying a bit of a wandering pattern there so there's a sphere there again and it should jump around to higher elevations as well so give it a sec there we go much better and you notice it's going around the walls all right there we go now it's going to walk around the walls again find its path so so we know that it's actually working there we go okay so that is just one simple behavior of just following it actually has wandering built into it let's add a little bit more to it let's add a little bit of a challenge for our cube so the cube actually has to start in random positions instead of you know where it is where it ends up uh it's going to be thrown into some other location and that this way we'll build out our tree at the same time so let's stop this i'm just going to hit ctrl s to save my game here all right so now let's open up behavior brx's editor so i'm going to window behavior bricks and editor click on wander so this is our wander we just had the one move to game object that we we had here so far so now let's add to this thing and make it a big tree so what we want to do is we want to have our our game object move to another area and then move to this game object area here so i'm going to add another brick i'm going to type and move again and this time i'm looking for move to position i'm going to put this about here now what i want to do is i want to specify a position within a specific area so i'm going to get a random value from the area so i'm going to right click again and type in get and i'm going to say get random in area okay so i'm going to put this here all right and so the idea is that i'm going to get a random number or a random location in a specified area basically inside our maze i'm going to pass that to move to position so that it moves our cube to that particular position and then it will do the move to game object but i want there to be a bit of a delay okay so i'm going to add one more brick here in within this sequence so i'm going to say wait 4 seconds so i'm going to put that here okay so this is basically the order in which things are going to happen all right so we'll say get random an area then move to position then wait for seconds then move to game object now in order to do a get random and area if we look at the node behavior it's looking for as an input if i if i say blackboard here and add a new parameter we'll call it wander area okay now the thing with this wander area is that it actually needs some so some sort of area to work with here so i'm just going to do a control s this stuff automatically saves anyway but just for the back of my head i'm just going to say ctrl s to say i'm going to close this what we're going to do is we're going to add something to this maze here just an invisible game well it'll be kind of invisible here we'll do up a quad and i'm going to put this quad i'm going to make its position a b just say 1 all right and the rotation will say 90 degrees for x sorry it'll be 90 degrees for y we have to basically get this thing to lay flat right now it's sideways there we go so so the x was the correct one all right and then what we're gonna do is we're going to stretch this out to be the size of the area here i'm actually gonna uh so we're gonna use the scale tool sorry that's the wreck tool we're gonna use the scale tool there it is and i'm gonna stretch it it's going to cover the entire area okay so that looks that looks about good i mean we could stretch a bit more all right now what i want is for this to be a little bit lower say 0.1 there right so at least we it's uh it's just below the walls all right but we basically have this quad it does blend in with the with the floor so it's okay you may want to work on changing the material if you were using a different implementation but but this is good enough for now all right so this is our area here that we're going to work with and uh you'll notice that if i click on cube we have wander area which is basically our new quad that we just dragged in here all right so now it'll base it on this area now let's go back so it's going to save this control s to save and then go back to my behavior bricks window or editor okay click on wander again okay so we have our if i look at node we have our wander area it's specified i want this thing to move to a position based on blackboard and i wanted to receive that position now in order to do that i also need to create an output parameter for get random areas already have blackboard random let's keep it as random position hit create now i have here random position as another blackboard area if i go to blackboard you'll see that i have different variables random position is an out parameter alright so now i have my input wander area my random position out area if i click on move to position now all right its input will actually be the random position so you're seeing that if i if i recap this get random an area we'll get a random we'll get a random value within wander area which we dragged and dropped our our quad into and it's going to produce an output value all right called random position all right move to position will now take that random position as input and actually move the cube to that random position all right i want it to wait for let's say wait for two seconds all right we'll wait for two seconds then go on to move on to the game object all right now here's something interesting is that we have these bricks here but they're all separate and there's this letter r here in this corner that specifies this is the root brick so that means this is the first brick that is ever executed all right now we don't want this to be the first brick right now because it'll basically just execute only this brick we need to put this into a sequence object or sequence brick so i'm going to right click see if i can first off just drag this down so we have more space i'm going to right click and look for sequence so a sequence node alright so there's sequence and now what i'm going to do is i'm going to connect these up so i'm going to connect this to here all right and then i'm going to connect this to here and connect this to here and then finally connect this to here now you notice that there's numbers here one two three four all right so it's going to run the sequence in this order but it'll run the sequence once i want the sequence to go over and over and over again so i want to add one more brick called repeat and it's actually called repeat decorator so we have here repeat decorator we're going to connect repeat decorator to sequence now you notice that repeat decorator now has the r the root nodes is going to execute this first and then go to sequence and then go to get random an area move to position wait for seconds move to game object now if i click on repeat it's saying minus one which means repeat forever and its policy here is continue even when a child fails so even if one of these child objects fails just keep going over and over and over again all right so now we have a behavior tree for these two bricks so it's going to repeat over and over again this sequence which consists of get random and area move to a position wait for a couple of seconds move to the game object so let's hit ctrl s here just to be sure let's give this a run now and see what happens and so there you have it uh you notice that the key was placed in a different position right away and then at the same time that the at the sphere also was placed in a different position and so now you have it now it's actually following it around it's following the sequence and and and so on all right so there you have it this is uh this was our crash course into learning about trees behavior trees and learning how to use a particular asset a free asset out there called behavior bricks there's all kinds of ai assets out there some are paid some are free and this is a very good one i like how it implemented wandering as well and it looks a lot more realistic than the wandering we did a couple of videos ago so it's a nice it's a nice tool to have all right well that's it for today uh don't forget to like share share with your colleagues and uh look forward to seeing in the next tutorial alright until then take care and bye for now you
Info
Channel: Jawaad Sheikh
Views: 135
Rating: undefined out of 5
Keywords: learn to code, game development, unity, unity3d, artificial intelligence, ai, behavior trees, trees, decision trees, navmesh, obstacles
Id: kX-jbY8063k
Channel Id: undefined
Length: 26min 29sec (1589 seconds)
Published: Wed Nov 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.