Unreal Engine 5 AI Patrol and Chase Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this Unreal Engine 5 AI Patrol and Chase tutorial we're going to create an enemy AI which patrols the map and then chases the player when the player walks close to that enemy AI so here I have a project and if I press play this is just a simple project where I have some basic character movement and I can walk around and we're going to create the enemy for the enemy I'm using this pack here from the Unreal Engine 5 Marketplace the city of brass enemies and this pack is for free if you wish to download it you can add it to your project or you can simply use your own project if you're interested in in-depth game development courses please visit my website pixelhelmet.com which you can also find in the description below now in Unreal Engine for AI to move around the map we need to add something called navigation mesh bounce volume so you can click up here go to volumes and add this one called navigation mesh bounce volume now for this volume here you can drag it down with an a move tool and just hit the ground with it now if you press p on the keyboard you can preview this navigation mesh bounce volume so clicking on P you can hide the preview and click on P again you can view it so this area defines where the AI can walk around inside of your map and to increase this area if you click on it and go to the details panel you can increase the X Y and Z so I'm going to increase mine and when you are satisfied you can click on this one here it creates it automatically the recast nav mesh this one if you click on it and go down here in the generation you can actually edit settings here so you can adjust how far up they can walk here and also adjust the distance between these these objects here so they don't get stuck for example this agent radius I can increase it and for example if I say 500 you can see it it increases the radius from the objects so obviously this is too large so if you write 50 60 70 you can see it gets further and further away from these edges so you can play around here and trying to adjust it and when you are satisfied let's continue to create the enemy I'm going to right click here making a new folder called enemy and inside of here right click blueprint class and now let's select this character blueprint class I'm going to call this one BP enemy going inside of this blueprint let's click on the mesh let's go ahead in the skeletal mesh asset let's add an enemy I'm going to use from the pack this one called Sploder and for this one I have to drag it down and realign it so the best way to do this is going to the right view over here so not the perspective View and I'm going to readjust this capsule here so it fits my character so the capsule hides I'm going to increase it slightly taking this character bring it down if your character snaps around you can disable the snapping here you can see now it's enabled now it's disabled and it's a lot easier sometimes when it's disabled so you can adjust them as you like okay something like this I'm satisfied clicking on this capsule I can decrease the sides here something like this so going back to the perspective mode I now need to add an animation so clicking on the mesh again over here instead of using an animation blueprint I'm going to use an animation asset and clicking over here I have an an animation it's called passive walk so I'm using this one called charger as work from this pack here and also for the walk speed for the character I'm going to increase this guy's walk speed and the character movement going to the max work speed and I'm going to set it to something like 130 that's up to you how fast you want your enemy to walk around so now to make the enemy walk around let's go to the event graph and inside of here let's delete all of these standard events let's right click and make a custom event let's call it move to random location and we're going to use a function called if you right click here and search for get random location in navigable radius and what this means it's going to get a random location inside of this green area where you place your navigation mesh bounce volume so it's trying to find a random Point here and it's going to make that enemy walk so get a random location in navigable radius for the origin we just want to find a point from wherever this enemy is standing so let's right click and say get actor location so this is the origin where we will start searching for a point for the radius let's just say we're searching within 1000 units now the next function that is very important is a I move 2 this one here and now we found a random location we want to move to now we have to move this AI so this is the destination this is the random location equation we just found and it's going to ask who are you moving I'm going to drag from here and say self because we're just moving this enemy here this is a self-reference and when it successfully moves and reaches that point this random location you have given it I just wanted to find another location and just keep moving around so I'm going to drag from here and just call this event again move to random location okay so we found a random a random point at the navigation mesh bounce volume and then we move this Ai and it when it moves and reaches that point it found it's going to move again it's going to find another Point all right so in order for this to work remember we have to call this event somewhere in the code else your game doesn't know when you want to run this code so we have to run this event somewhere so let's right click here and say begin play so let's use the begin play event the begin play is simply what what happens if you begin playing the game I want to move to random location I want to move my enemy to a random location so it's going to run this code and my enemy is going to find a point move to it and go ahead and find a point again and loop this over and over again so let's go over here I'll take my enemy place the enemy on the ground going to click play and now you can see here this is my enemy my enemies walking around he's going to find a new point when he reaches the point he already found or the chasing let's open up the enemy and let's click add on the components and search for Pawn sensing so this one Pawn sensing click on it so for the pawn sensing we have to adjust this peripheral vision angle so if you click on the viewport you can see here in the viewport the angle for his vision which he can see us so you can increase and decrease it you can see what's happening to see it better you can place the enemy here in the level and trying to adjust this peripheral vision to your liking maybe something like 75 for me and you can also adjust this one if you want to the side radius that is the radius in which the enemy can see you so this one is adjustable this one as well so let's go back to the event graph and over here if you click on the pawn sensing you have this event down here on C Pawn so what happens when the enemy sees the player so I'm going to drag here and say sequence I'm going to make a sequence node which just organizes my code so first what I want to do I want to take my mesh here and I'm going to say play animation just like that because I want to change my walking animation to a running animation so my enemy runs after me so here I'm going to search for Sprint and I'm going to choose this one called charger alert Sprint and then I'm going to take my character movement for the enemy and I'm going to set the max walk speed so the enemy walks faster and I'm going to set it to something like 600 so now the enemy is printing after us next what we want to do again we want to use this one called AI move 2 so now we change the animation we change the speed of the character or the enemy now we want the enemy to chase after us so let's strike from here and say a I move 2 so we're going to use it again now again who is moving this is the enemy moving so let's right from here and write self so a self-reference again and instead of using this location this random location here as the destination now the pawn the player is the destination so let's drag from here this is the player and the player is the target actor now with this code the enemy is going to chase us forever but I don't want the enemy to chase us forever if we hide a place for example behind the wall we want to the enemy to go back and it's like the enemy never found us so I'm going to drag here and say re-trigable delay and now you have to specify for example if you hide behind the wall for let's say two seconds then the enemy will lose interest in you after 2 seconds and go back to moving around randomly so after two seconds for example you can put this as much as you want after two seconds let's take this here the animation and the max walk speed and let's change it back to whatever we had it before so here that the passive walk let me click here search for Passive walk and for the movement speed our default movement speed is set to 130 so let's set it back to 130 and then I want to again just move around like I did before so let's call move to random location so there is a slide bug and I will show you what the bug is but let's click on play so now the enemy Chase is after us if we go in front of the enemy however we can see the the animation is lagging a bit and this is actually because it is playing the animation again and again because if you click on the pawn sensing this sensing interval this running animation is changing every 0.5 seconds so two times a second this is changing and we don't want to do that so we just want to change this one time and then it's going to chase us and when it's finished it's going to walk around and then we want to do it again whenever it senses us again so let's drag from here and say do once so we're only going to do it once and not 0. every 0.5 seconds okay so it's going to do this once now and we have to reset this at some point so let me make a new custom event called can Sprint again and I'm going to connect it here and then I'm going to call this one whenever it stops chasing us so over here just right before moving around I can say can Sprint again and with this custom event we're going to open this do once this is what reset means it means you can do this again so we have to reset this before it can work again so let's connect this back here and if you want the animation to be less laggy you can click on this Pawn sensing and set the interval to 0.1 for example just remember it's going to take more resources because now it's updating 10 times a second and for these animations let's click on looping and looping just to make sure they are looping compile and click on play and let's see what happens so now they're just walking around and whenever they turn around and see us they will chase after us just like that and we can run around you can see they're chasing us if I run very fast and hide behind the wall they should lose interest after two seconds and you can see they have lost interest in us and you can also adjust the value of how far they can see us right now he cannot see us because the peripheral vision is set like this now this guy saw us if we run away for two seconds Big Guy cannot see us anymore I hope you've enjoyed this tutorial if you like this content please consider subscribing and setting the alert on so you can see more useful tutorials from this channel again you can visit pixelhelmet.com if you want in-depth courses or if you want me to help you out with your project you can also go to dip my Discord and you can find everything in the description below I'll hope to see you in the next tutorial
Info
Channel: Pixel Helmet
Views: 28,759
Rating: undefined out of 5
Keywords: Unreal Engine 5 ai patrol and chase, Unreal engine 5 ai patrol, Unreal engine 5 ai, unreal patrol path, unreal ai patrol, ai chase player, ue5 ai, unreal engine ai patrol, unreal engine 4 ai patrol, unreal engine enemy patrol, ue5 ai patrol, ue4 ai patrol, unreal engine 5 behavior tree, unreal engine behavior tree, unreal behavior tree service, unreal engine 5 enemy ai patrol, unreal ai behavior tree, behavior tree, ue4 behavior tree patrol, ue4 ai move to, ai perception
Id: lbqZS-cgcQs
Channel Id: undefined
Length: 12min 36sec (756 seconds)
Published: Sun Mar 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.