How to Make a Simple Behavior Tree in Unreal Engine 5 - Advanced AI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys welcome to new Unreal Engine 5 tutorial and today gonna show you on how to make your first behavioral tree it's gonna be a very easy video to follow so let's get started but first check out the link in the description to get out with some Unreal Engine courses on winbox alright so the first thing that we're going to do is go ahead and create a new folder in my case it's going to call it Ai and the first thing that we're going to go ahead and create is right click go into artificial intelligence and create the behavior 3 asset so I'm going to name this like BT underscored EI for example now the thing is that we have to also create another asset that will basically emerge kind of with the behavioral tree so we will also have to create under artificial intelligence a Blackboard so we'll call this like BBD underscored um aio2 okay so basically the behavioral tree will be of course the logic itself of the AI and the Blackboard will be like a series of um variables that will be able to connect uh from the vehicle 3 to outside the behold tree now you will understand it better when we start actually playing with it so we're gonna go ahead and open the behavioral tree I'm going to put in here so you will see that it looks a lot like I guess blueprints um but later on it's going to be a bit different so the first thing you have to make sure is that under details you have the um Blackboard that you created for that AI selected and you can also toggle between them up in here so it's really cool okay so we will have first of all the root so basically we have wood trees the way that they will function and flow will be from uh from up to down so kind of downwards and from left to right that's how it will always be so the first thing I'm going to do is go from the root and make a selector so your selector will be able to go from different branches over here and basically just pick one of them depending on what uh you know conditions there are so the conditions will be done with decorators that will be seen them a bit later in the video so um then we will have basically a sequence that basically will be able to play different tasks in a number of sequence again from left to right and you can just organize them for example the first one that we're gonna have is gonna be um just look around so we'll basically have a sequence of looking around so here we have a task of picking a spot moving to that destination and waiting three seconds so the selector then will pick if gonna choose look around or another sequence that's gonna be there that another sequence that we are gonna have is gonna be Chase Chase player so basically that's really their function and sequence will just play different tasks um in from left to right and then Chase player again I mean it's a sequence and then the selector can choose between different sequences depending on a decorator that will basically see it in a second so first of all let's go ahead and make the look around movement now for that we will basically need to create a new task because you have to basically create tasks in order to like give it the the actual logic to the blueprints straight to the behavioral tree but there's also a series of statutory created for us so it's really cool for example the move 2. now the thing is that you have to give it a a position to go so I don't really like uh you know using this um you know this task so what I like to do is create a new one so we're gonna go up here into new task and we're gonna open the AI folder I'm gonna create a new one it's called basically tasks so here we're gonna basically replacing all the tasks for this AI so then we're gonna delete this first part so we got BBT to desk and then it's going to be just um basically uh roam around or something like that basically it would just pick a random spot and move into that so this will basically open what it is pretty much a blueprint so now we have to basically add some functions into it so we can just go into functions all right and then the first time they're going to use is receive execute AI this is basically like the begin play and will give us the owner controller and the controlled Pawn basically and then we need to say when the a uh this task will basically finish so it can basically move into the next task in this sequence because not it will just be stuck which is a common error made in people you know starting out with behavioral trees so we have to basically call the Finish um execute and then we will by default always pass a success and the thing is that the success would not change whatever whether it will just continue or not it's just knowing if the task was successfully complete now because we're starting out and we're not gonna you know be building more complex systems we can just always lead this success on and it's gonna be okay okay guys so the first thing to do is just make a simple AI move too so basically what the note that I well the task that I added before so the uh move two pretty much this but inside my task then I'm gonna create so I can hear past the destination uh more easier so in here in success we're gonna put the Phoenix execute the pawn is going to be the control of Pawn it's gonna be like so for example and then the destination will basically be a find random wait sorry random point in reachable radius there we go get random return Point arrays that's it so the origin will basically um be the get control Pawn get extra location and then we'll plug that in I'm gonna go ahead and move all the notes um to the right it's a little bit more organized that we go and then the the radius basically the distance that will be able to pick a point it will be for example I don't know a thousand so it will pretty much cover the whole scene I guess and that's pretty much it the acceptance rate is going to increase it like to 120. this is just the distance at it will the air will basically stop when it reaches the destination and normally it's like too close so like to leave it at more of a space and there we go we'll basically have our fast uh first task for our AI so we can just go into the look around sequence plug this in find our task which basically was uh they were PT just roam around um of course you can remove the BT task at the starter anyway I just let it and then we can just go ahead and add another thing here oh it will be wait so yeah there's already another handy task already create for us how can you say wait two seconds so like I mentioned before it will go from left to right so basically go into a point and then wait two seconds in this sequence okay now the thing is that it will never go into Chase player right now because um well you know we first of all don't have anything uh and then there's no condition for it to go into this one so let's first of all uh test out this AI look around the stuff so first of all gonna go ahead and control shift s to save everything and we have to go now and create a new blueprint class it's going to be a character of course to be able to create an AI so it's going to be a BP underscore AI for example so let's go ahead and open this up now let's go ahead and give it a mesh so I'm gonna just for example get the many simple and let me just go ahead and put it in a nice position here in rotation there you go and I'm just gonna go ahead and we're gonna create a new uh also animation blueprint in a second because I I always like reading my own my own animation blueprints because I can just control them a bit better so we'll do that in a second and then that's pretty much all water we need for here now we have to create one more thing so we have to right click go into blueprint class we could go in all classes and search for AI so I'm going to go ahead and create a new AI controller so select this and then this is going to be um AI underscore uh controller for example you can of course give it whatever name you want now this will be kind of the brain that will um basically be on our blueprint and it will basically run the behavioral tree and stuff sorry bo3 here so go ahead and open the the controller we're gonna go into the van graph we're going to delete the event tick we're only going to need the beginning play I'm going to say run we have a tree and then the target will be so but the asset will be our behavioral tree that we created so now we can use compile and save and close now we're going to go back into our bpai and then I'm going to go into class defaults and search for AI I'm gonna go down and you can see the AI controller class well we'll just go ahead unplug the one that we have just created which is AI underscore controller I didn't give it a pretty good name but anyway now I think that I always like to tune in before we forget is the auto process AI instead of placing Walt I like to place it uh place in world or spawn because if you spawn an enemy it will not move and you'll be like what is wrong what is not working and it's really just this um variable over here so it's always a a pain um so we can just drag our bpai on the scene but the thing is that now we have to basically tell Unreal Engine all the spots where the AI can basically move so what we have to do to tell Unreal Engine about you know where it can go is just go here go into volumes and create a nav mesh Bunch volume so we're going to do is reset the location over here we're gonna lock this and put this as 50. so basically be just a huge Square as you can see just covering in the whole scene and now if I press P you will see how it will basically start to generate all the paths where the AI can basically go really cool this control shift save and now if I were to press play you will see that the AI will start to pick a random location wait two seconds and then go into an allocation with seconds that you can see so and we go to the to the behavioral tree you can see that it's basically doing that it's going to look around and then waiting two seconds and then again roaming around Etc so everything is working as intended cool so let's quickly just go ahead and create the animation blueprint for the AI because I like having you know the animations so it's got great and folder for animations right click go to animation and first of all I'm going to create a blank space 1D so this is going to be the SK mannequin and it's going to be the AI button space go ahead and just open this up real quick so the horizontal axle will be speed and then the maximum speed will be 600 and then we can use basically get the idle animation there we go and just put it on the left you know also say it's not too weird and then the uh run animation will be into the right there we go so basically a you know plant space I have a dedicated tone on this so that's why I just cover it real quick but basically it will just allow us to blend between and transition in between uh different animations depending on my value that in our case will be the speed so now we can just go ahead and close this and now real quick just create an animation blueprint with the same skeleton and then this will be ABP underscore AI open this up and then we're going to get our AI blend space is plug in a default slot so it can later on play animation montages and attack and stuff then right click I'm going to quit speed now going real quick because I've already done a tutorial on this so that's why I'm going very fast I'll be leaving it in the description don't worry about that but yeah so now we have to set the speed so we can just go into the band graph and then I'm just going to make it real quick so cast to the AI BP Ai and then just do this and then I can just get the velocity so be down here so this is a vector so we have to convert it into a vector length which is here so we have a float again just plug this speed over here there we go perfect we can just compile say go into the blueprint go into the mesh and assign that abpi that we have just created that um has some some animations now the thing is that it's not basically working right now because you can see it it just unloaded the AI and the nav mesh now why this is happening I don't know I don't know what's happening but since I switched into 5.1 sometimes it just uploads my stuff I think of War politician but just save everything and then you can just reload the third person map and then it'll be back in so you can see the imovs with um with animations now there's a really Snappy rotation that we're going to fix so we're going to go into the AI go into class defaults search for draw and we're going to disable use control rotation yeah it's just gonna rotate the character on a very Snappy way so what I want to do is go to the character Moon component and change this to be use control design rotation and this will just do the same but just rotation make the rotation very smoothly okay oh we can now continue with our Behavior so now what we want to do is make sure that if we are seeing the player we will switch into the chase player's sequence so first of all let's make the task for changing the player I think it's going to be a bit easier so I'm going to go ahead and click new task new task and then we can use um for example click the room around that we have created this just really doesn't matter I don't know it really was there is to fill the name anyway go into tasks maybe the task um Chase player let's go ahead and open this up now again in the function I'm going to override the receive execute AI select the gameplay and I'm going to add the first finish execute and then put this in success so I'm going to do pretty much the same AI move two and then the this is going to be on success I'm gonna finish the execute and then the pawn is going to be the controlled pawn and then the entire actor will be the get um player character and then the acceptance rate is going to put it up into like for example a hundred and there we go it will just go ahead and move into the player now it will only do this if we are so we we only need to do so we are in the chase player root so first of all let's go ahead and just add the chase Player thing and the thing is that we have to add a decorator so what is a decorator well a decorator will be just well we can just start adding so we can just uh right click on here and look around yo into app decorator and we can choose different sections of decorator now the one that we're going to be looking today which is the one more generic and use it's gonna be the Blackboard one so now if we hit on this decorator they have they has added we can add a um a notify Observer so basically when the result changes then when it aborts so basically when a variable that we're gonna plug in here changes we're going to say what is gonna abort and it's gonna say in which point it will again um go up and see in which route it goes and it makes sense so if we put for example self you can see this highlighted so it will exit this sequence and go up into selector and then it will double check again in which branch it basically has to go depending on the value but if we were to set this into both you will see that it will also consider this one and then exit back in here and consider both ones so in my case I'm going to say itself because I just want to basically into the self and just exit this sequence and then we need to say that um if if a barrier if a variable sorry from the Blackboard is set or not so right now this probably didn't make a lot of sense but don't worry first of all let's create the Blackboard variable and you understand much better so let's go into Blackboard we're going to add a new key this is basically just creating a new variable for our Blackboard sorry for our behavioral tree so this is going to be a bull and this um has seen player and then we're gonna leave it as default it's save and then go back into the vehicle tree so now we can basically click on the Blackboard that we have created and go into the Blackboard key and say has seen player and then it's not said so basically if it's if it has not seen the player it will basically be in this sequence but if this value changes and is now set so it has been the player has been seen it will exit this um sequence um and then consider in which branch to go depending again on the value so now we can go into Chase player add another decorator a Blackboard decorator and then we can select this and say again on self and then if it's set so yeah it basically is seen the player and there we go we can just go ahead and Save and now we have to tell the the AI well basically uh where this variable we enable not when we have seen the player so we're gonna go into the bpai and we're gonna go ahead and add a pawn sensing which is here component now we can just see a lot of variables over here so first of all the the angle of vision I'm just going to put it into 17 it's a bit uh shorter and then the side radius like into 500 so it's a bit smaller actually like 700. so basically We're Not Gonna touch citing sensing and stuff only the sorry the the hearing and stuff we are only gonna see right now site which is the radius the distance that you can look and then the the the angle okay so basically when it sees upon what we're going to do is basically cast to the third person character so basically we're just going to continue if the pawn that we have seen is the player We're not gonna do anything for the player right now anyway let's just get the AI controller because as you know the the Blackboard was in the air controller and what to do is basically get the Blackboard and then what we want to do from here is basically set a value as a Boolean basically this is just accessing the Blackboard and it's gonna be touching one of these booleans on on the Blackboard in that case we only have one so we have to pass the key name basically the variable name so it's going to be make little name so here we can just type one and the one it was hasi in place so I'm just going to go ahead and copy this go in here and just paste it so it's perfect and then the bull value will be it will be true because it means that we have basically seen the player and the very last thing that I just I nearly forgot is the Control Act 2 will be itself because basically is the blueprint itself is the AI basically so now we can hit play and it will just wander around until it saves me as you can see and will start basically following me as you can see now I'm going to do a change here and it's the character model component the max walk speed I'm going to put it like in 450 so not like run to me and then stop run to me and stuff so you can see that it hasn't seen me so just wandering around you know just looking forward roaming around but it when it sees me it will start to go ahead and chase me and as you can see if I go into the Blackboard into the be able to you sorry you will see that it's now in here so yeah that's pretty much any brief introduction into the Hebrew trees as you can see it's pretty simple when you start to narrow it down into different processes task so yeah if you found this tutorial helpful I really appreciate it looking you know like the video and subscribe to my channel I have lots of Unreal Engine 5 tutors this one so if you want to check it out go ahead now yes Vlog said bye bye [Music]
Info
Channel: Gorka Games
Views: 111,974
Rating: undefined out of 5
Keywords: games, videojuegos, unreal engine, unreal engine 5, ue5, behavior trees, tutorial, quixel, megascans, unreal engine behavior trees, behavior trees artificial intelligence unreal engine 5, ue5 behavior trees ai, ue5 ai behavior tree tutorial, ue5 how to make a behavior tree, unreal engine 5 simple ai behavior tree, unreal engine 5 how to make an ai with behavior trees, unreal engine 5 easy ai behavior trees tutorial, ue5 simple ai behavior tree tutorial, behavior trees ai in ue5
Id: QJuaB2V79mU
Channel Id: undefined
Length: 20min 46sec (1246 seconds)
Published: Sun Dec 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.