How to Make an Enemy AI Using Behavior Trees in Unreal Engine 5 #2 - Attack Player

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys welcome to new Unreal Engine 5 tutorial and today I'm going to continue with our AI enemy using behavioral trees it's going to be a very easy beautiful so let's get started but first check out the link in the description to get out with some unreal ending courses on winfox alright so as you may remember in the last episode we made it so that the AI will go ahead and pick a random point in the scene and when it detects us with a certain uh vision and radius it will go ahead and start chasing us so now we want to do is basically go ahead and make him attack us when he gets into a certain distance from us so for example you guys very close to us he will now start attacking us with this animation and then send us damage so let's go ahead and open its behavioral tree and what we want to do is basically go ahead and create a new task let's go all Heavy up here and then go ahead and select BBT test blueprint base and I want to go into tasks and then just go ahead and leave this so BTT ask underscore well attack player let's go ahead and open this up so we're gonna go ahead and go into functions and then in the upper right we want to do a receive execute AI as you may know from the last episode this is pretty much the gameplay of a task and now we want to go ahead and find the Finish finish if I spell it right finish execute and then for now we're going to go ahead and take its success alright so this uh the note that will determine when the task will finish so we can move for example from this one into the next one you know how it works alright so what we want to do is basically detect and make sure um that we are close uh to the player all right so actually we're going to do all that kind of uh stuff inside of the blueprint why well I I personally think that is a bit more Optimum to do that all right uh I think that making the attack system and stuff is one thing that the blueprint should do all right so what we can do is go ahead and get the control pawn and do a cast to our BP underscore AI which is basically our blueprint for our Ai and then we will go ahead and call a custom event over here and then it will go ahead and finish the execute this compound safe of course we haven't been for this we have to call that attack event but let's go ahead and quit so let's go ahead and open the blueprint and then let's go ahead down over here so you may know the code from last uh the last episode when it sends the player will go ahead and set the Blackboard and variable through so we'll start chasing the player so what we're going to do is make a new custom event which is well attack player and now let's go ahead and compile and real quick go back into the task so now from the um cast will be here we can use call oh the uh attack player event that we have just created so now we can go ahead and finish this like that all right so for now the the task we can close it all right let's go back into the blueprint so we want to basically detect if we have the player in front of us so to do this we're gonna go ahead and get the sphere trace for objects all right so this will basically create a simple sphere and we'll detects if there are any objects all right in our case we'll detect part player so the start Position will be well just in the center of our object so just get in the actual location and then the end point will be well the uh pretty much the the forward vector so you want to look and to find where this enemy is actually looking I want to get the four Vector so basically uh the angle as it is facing forward okay and then what we want to do is multiply this value and right click real quick over here compared to pin and to float and this is going to be the distance that we will be able to uh get our sphere Trace so in our case too for example 250 so this is pretty much the distance that the enemy will be able to attack and detect the player so now we want to go ahead and add both of this just hold Ctrl and then also go ahead and drag the button into the pin so I think it's a bit clearer than that and then we can plug in the get actor location and then that will be our endpoint okay and you will see how it looks in a second okay now the radius is how because this is a three uh three dimensional sphere you know we need a radius so for now I'm going to make it 120. this to you but we'll see how it looks in a second so can I get how you know how big you want and the values and stuff and then real quick in the get for a position one to get the actor rotation uh there we go here it is all right so we can also put this over here it's a bit neither okay so now the object types well we're gonna go ahead and make a rate and this is just selecting uh which type of power object we want to be able to detect in our case it's going to be pause because normally characters in Unreal Engine are pawns okay and our third person got the blueprint will be one so then we want to go ahead and get the return value and bring the brush so it will only continue if it actually has detected an object so let's go ahead and get our out hit and then break it so now we have all the different parameters from the adapter that we have I'm basically detected so we want to make sure that it is the player so we're gonna get the hit actor and then cast two sorry let's pull it wrong yes two third person character and there we go so if the cast success it will go through this first pin over here and it will be the player but if the cast fails it will go through the second pin and well you know it will not continue so this is a great pace of doing this just detecting what we're you know detecting the side shouldn't be a player we can also do tax and stuff but I think this is the fastest way of doing so alright so now what we want to do is use for now uh we'll do all the things uh playing animation stuff in a second but right now I'm just gonna go ahead and print a message in the screen so we know we are attacking right so attack this is just for us to know that everything Until Now is working now I'm gonna go real quick over here I'm gonna go in to draw a debut type I'm going to type for duration so this is uh just for us to see how this um Trace will look in the game and we can see the radius and stuff and how it looks and if it's optimal for us all right so now we actually want to call the test that we created earlier so we can call this event that we have just created from our Behavior tree so in our case we want to do it when we're chasing the player and we actually want to do it always that we're chasing up there so there's a very handy uh kind of sequence for it which is a composite and it is a simple parallel so this will pretty much do um two tasks asynchronous so we want to type the main one onto the left which are well the chase player uh task and then we want to get our new one which we have created which is the attack player task so now we can kind of Center up here and we'll do both at the same time of course this will pretty much spam the attack um bottom or another of course the attack button because it's not a player interacting but you know this spam attack of the air you know what I mean anyway so now just go ahead and double check the print over here in the left corner which will be attacking or you can even see the sphere Trace appearance so now you can see the sphere Trace as you can see and now when it detects my player when it gets into a certain position you can see the now well it is going ahead and saying attacking of course like I mentioned before it is doing I don't know of hundreds of spear trades because of course we haven't prevented him of spamming and now there are many ways of doing so okay so we have to wait we can directly implement it in the event where we will basically make sure that um the trace once the event attack event is caught we'll do the trace and the uh cast to the character Etc uh so often and with a frequency or like we're gonna do it inside the actual task so we will basically only call be attack player um event uh when we actually won so let's go ahead and go here and this will be as simple as going and dragging this and saying delay as simple as that okay so now we can just put a duration now of course let's go ahead and try this this is how you want to do it but I'm gonna put one over here so if we press play you will see that it will only do with each one second or so often I'm gonna actually decrease the time a bit so it's a bit faster like 0.7 maybe this is just you know personal preference so this is pretty much experimenting and see what you like but now you can see that when he starts changing he will do uh one and then another one and another one and it's just a bit better as you can see right so now what we want to do is play the attack animation and you know send damage so in my case I have got my attack animation and here as you can see so this from my pack that I always use in my combat tutorials so I will be leaving it in description it is starting with me alright so we you know Link in the description so you want to put it as an ue4 mannequin because well for the skeleton of the four biggest actually was made for the ueforce captain of course guys you can use whatever animation you want I'm just using this one to showcase for the tutorial yeah so we want to go ahead and now this animation to use the new ue5 skeleton as you can see so it is as simple just right clicking go into retired animation answers duplicate and and then selecting the ik which is ue4 to ue5 and then go ahead and get the folder that we want in our case my one is over here yeah I missed and then pressing we Target and I usually don't have with the new ue5 mannequin so now we have the uh attack animation play just ignore the shotgun it was from another tutorial stuff okay anyway so we can now also close make sure that you save everything Contour shift s and then we can close our task and we want to go into blueprint but first of all we want to make one more thing into our content browser and just create an animation mounted so just go ahead and right click into the animation go to grid and then mount it and now we can go ahead and have this anime monster we'll pretty much look uh identical but the thing is that we can call it whenever we want from a blueprint so now we can go into the blueprint of the AI and we can delete the print string and now what we can do is instead of doing that we can call this knowledge play a name montage all right and now we can now select our um what was push I think yeah a purple push attack one yeah okay I I it seems that I already imported from another tutorial but it's okay you can use whatever you want no notifications please there we go all right so uh now it will go ahead and play B animation uh when it gets into a certain point but let's go quickly and make sure the inter animation blueprint that we go into the and then graph and we have the default slot the default slot is the uh it basically where the animation Montage will play by default of course you can create new slots but the animation Montage will play on here so if your animation blueprint doesn't have the slot go ahead and add it right now default select but we did add it in the last tutorial so it probably will happen okay all right so now if we press play you can see that it will let me get near to him he will go ahead and find us and now he's basically going ahead and attacking us as you can see each time he launches the thing so we have a few things going over here first of all you can see that my camera clips through him when he attacks sometimes we don't want that so we're gonna go ahead and go quickly into the uh blueprint so that the mesh go down into Collision preset and then in here instead of character match going to say custom and in camera we're gonna pass it into ignore I'm gonna do the same thing as the capsule component let's go over here into collection presets custom leave everything as it is except for camera we want it to be in ignore so now we'll basically just ignore the camera booms trays to detect objects and therefore it will not do that thing now we can also get rid of the Spirit race we pretty much don't do it anymore but there's a few things that are happening right now and it's pretty much that he says pretty much skiing you know what is happening his skin now I do have a really good tutorial on that all right and that I will be leaving in the description you want more detail about it um as you can see but we're gonna go ahead and do it um real quick over here so what we want to do is go and open up our animation blueprint and we want to put this over here and then after everything right before the output post want to go ahead and call this now which is planned pulses when you say what you said playing poses per bone but sorry blend the layer uh purple I was freaking okay so um basically this will allow us to split and blend uh two different animations at once all right like I said I will not really explain it right now because I have a tutorial on it so I'll go ahead and leave a description so you can access it all right so the bass boost will be our main body like in this case the lower part and then in the top one it will be well when we play the animation so let's go ahead and get the default slot and just put it in the blend post and we want to get our blend space and just go ahead and create a new cachet it's like 7A in a pen drive and one to name this uh use The Locomotion and then in here we can put it a bit up so it's a bit more organized and then in the bass pause we can go ahead and directly use The Locomotion but in the Vivo slot we want to use The Locomotion and then add our Depot slot so maybe this does didn't make a lot of sense for you okay don't worry I can imagine people having a tutorial on this so if you want to check it out go ahead but basically we are saving our whole animation of a whole body in a pen drive okay using pen drive it's not really Panda of course but you just do understand a bit better and then we're separating the uh body in two parts we'll basically Define which two parts in a second but the base pose and the base uh band piece uh post zero the base pose will be our lower part of the body so in our case we will just want directly to play these animations but the upper part of the body which is the second input we want to play our uh lower part animations plus our attack animations alright so now we want to go and select the layer lamper bone and here layers it up we want to add a new memory here which will be our bone name so we can go directly into the skeleton and we want to split up into let's say spine two so let's go ahead and quickly copy the name go back into the animation blueprint select our name graph sorry control no this is the many we want to go into the AI all right and then paste the name over here all right so now if we press play you will see that now he'll be playing the attack animations but still be walking which is just much cooler as you can see great so let's quickly go ahead and implement the damage so let's go into the blueprint go ahead and say apply damage right after we go ahead and uh you know do our casting and play animation uh the damage Factor will of course will be a player which is our hit actor and then the base damage will how much damage you want in my case let's make 10 for example and right now I'm not gonna make a Whole Health System because I already have already have three I don't know even how to speak uh I need to turn on that so again I will be linking that in the description alright so let's go ahead and quickly use you know to get this working and it's going to go into the third person get the blueprint and use code this node I know I have a lot of notes over here don't worry about that uh sorry apply the damage now uh event any damage all right which already have a thing over here so let's go ahead and separate this so this will basically be called when uh we call this so when an actor calls this into an another actor the other actor Will detect by this I know about explain to many sense but anyway when we reset damage it will call this all right so in our case let's go ahead and just say brain string let's go ahead and say damaged something like that okay and then we can also print the damage damage okay we can just press play and you will see that but we we can we see the prince damage over here and it's basically one ahead now there's a thing going on as that well he just stops when he gets very close to us and there are a few reasons to this let's go quickly into the we have a dream go ahead and Ctrl shift save everything I'll just close all sort of thing except for the AI so you can see the chase player over here which has an acceptance radius as you can see and the thing is that when he reaches that point all right he calls the Finish execute um note which finishes this task now the thing is that this task plays asynchronous to this one so if this one stops this one will always oh sorry also stop so the thing that we want to do is go ahead and get just the asset and raise to zero and that should do the trick because so now if the player comes in Hebrew yeah I that didn't make the trick actually um so if you want to set did that back to 100 all right I'm going to go here and then we want to also go ahead and call the attack player always after the the parallel so basically in conclusion he'll be attacking and chasing us at the same time but just in case he finishes with this point if we're still in the chase player mode it will also continue to attack so if we now press play even though he gets to a point he will continue to attack as you can see so yeah that's pretty much what I did uh yeah I just pretty much you know make sure that everything goes from left to right so if this test finishes it will continue this one right it was a bit longer uh what I would expect it but it's okay because we have just reached 10K subscribers so thank you all so so much I really appreciate all your support guys uh we'll continue making of course I was on content go ahead and join the Discord that I have just opened Link in the description and now yes we're all set like And subscribe and see ya bye bye like And subscribe and now you just be all set I know I have repeated this in all my videos but bye bye [Music]
Info
Channel: Gorka Games
Views: 44,266
Rating: undefined out of 5
Keywords: games, videojuegos, unreal engine, unreal engine 5, behavior trees, tutorial, unreal engine behavior trees, behavior trees artificial intelligence unreal engine 5, ue5 behavior trees ai, 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, ue5 ai attack player, ue5 ai attack player behavior trees
Id: LL9hpaSLD5g
Channel Id: undefined
Length: 19min 55sec (1195 seconds)
Published: Fri Jan 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.