AI Sight Detection And Chase - Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another other engine 5 tutorial in today's video what we're going to be going over is creating up an ai in which we'll have site detection and then chase the player if it sees and detects the player nothing is going to happen in this particular version when the ai catches the player however i can do something like that in the future if you wanted something hit play is showing me today so the ai is just going to walk about mining so business until it sees us and as you see it is now chasing us the speed hasn't increased but i can do that if we wanted as well what i'm basically going over is the ai it's going to spot us and chasers and if it loses sight as you can see there it's walked off and you can also increase the time it takes for the ai to lose interest so i've just got it set to one second after it stops seeing us it will walk away as you saw there and you can have this as five seconds 30 seconds whatever it is that you want you can set it up and customize it very very easily for your own personal needs so this is what we're going to be going over in creating today so without further ado let me delete this code and i'll show you how i've done it so the first thing you want to do is you want to create your ai now if you haven't already got that don't worry what we can do is go to our character blueprint so here it is in content third person blueprints select it and hit control c then go to the folder you want your ai to be in and hit ctrl v to paste it there then you can hit f2 to rename it but i'm just going to delete it as i already have one then you can open it up and delete all the code and components which you don't need this just allows us to nice and easily already have everything set up which we want and in here what i've done is i've created this random rom code which you can see on screen here i also have a video which i'll leave a link to in description down below in which i go over setting up and creating this code here so once you've got everything else in your ai setup it's time to start adding in the site detection so we can go to the viewport add a component and we want to add pawn sensing as you can see here we'll compile and save that and then in here we can just change all the different values so obviously we have hearing as well we're not going to go back today so the site radius you can increase how far they're going to see so like this i think the default of 5000 is good and you can also change the angle at which they see so their peripheral vision so something like this you can really just customize this to get it perfect for you and what i like to do is place the ai in the level and have them selected so i can see this in the actual world so i get a good reference of what it's going to be so i'll have that angle and that far which i think is good for me that'll work so open this back up again and also with this it already has it built in for objects so it does have object permanence as well so it won't be able to just see you through a wall if that wall is there it has proper clusters and all that the ai will not be able to see you and that's all we're going to change obviously make sure c pawns is ticked but it should be ticked by default then we're going to scroll down until we get the events and we're going to get the on c pawn event here what we're going to do is out of this we're going to get a sequence node so you can hold on s left click to get that then 0 is going to go into an ai move 2 node with the pawn obviously being get a reference to self so it's moving this ai and the destination we're going to leave as 0 0 0 as we're going to use the target actor instead so the target actor can just go into pawn of the on c pawn so whatever pawn that the ai has seen that is now going to be the target that the ai is just going to go towards so wherever the ai moves to this will constantly be chasing that location and just be following the player so this is nice and efficient and a really good way of doing it however this is going to constantly follow the player even when the ai can no longer see us so how do we set it up to stop them doing that well out of then one we're going to get a retriggerable delay as you can see here and the duration of this wants to be 0.6 or higher so again i'm going to put mine as one the reason why it needs to be 0.6 or higher and the reason why this will actually work is because if we select the pawn sensing you'll notice our sensing interval is 0.5 so this event here will fire off every 0.5 seconds that the ai is seeing the player so it will fire off come through this if one second has passed and this hasn't fired off again it will fire off the code out of this because a retriggerable delay will restart from the beginning so if the ai sees us it will go to one and if they're still seeing us it will restart again continually until the ai has no longer seen us so again this value here you will change depending on how long you want the ai to be able to continue chasing the player before after they've seen us so again you might want a nice five or ten seconds just so to make sure that when the player walks around a corner the ai doesn't suddenly lose sight of them anything along those lines ready you just put this in here and you'll probably want to do some testing just to see what works perfectly for you but to actually stop the ai from chasing us what you can do is either stop movement immediately or you can do an ai move 2 and just have the destination as get actor location so it just stops them where they are but what i'm going to do is just call my random rom code so it's just going to go straight back into my random roaming now if you have patrol code you could put the patrol there but essentially whatever you want the ai to start doing when they stop chasing you put that there and that is pretty much it it's very very simple code to do so we can compile and save and again that's all we need to do so we change the settings for our site radius and peripheral vision and all that and we've also set up this code here to chase us until it's no longer seizures for longer than one second so we're going to close this and there is now one final thing left to do and that is just to make sure that ai can actually move in our level so we're going to go up to the add to project up in the top left up here go down to volumes and add the top one of nav mesh bounds volume and this again just means that the ai can actually walk about in our level so we're going to put this pretty much in the middle of our project and then just make this the size of the area which we have so that ai can walk around and if we were to press p we can preview what this is going to look like so anywhere that is green the ai can walk so this is going to be perfect for me so i'll press p again and hit play to test this out so again they're walking about they've seen me and they're now coming towards me you can see they are chasing me like this and if i to get out of sight for over one second they will stop chasing me as you saw perfectly there they started random roaming again which made them just turn around and walk away so if i walk in front of them again they should start chasing us until they can no longer see us here we go let me actually just be able to get in front of them real quick they keep turning away like this so they're now chasing us once again so i think that'll be it for this video as we've done everything we want to do we've set up an ai in which what it's going to do is be able to see us using site detection and pawn sensing and then it will continually chase us until it can no longer see us for longer than one second as you can see here although it continues walk around the corner away due to its path finding of random rome but again it can't see us so it's going to stop chasing us as you saw there so thanks so much for watching this video i hope you enjoyed it and hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see you in the next one [Music] you
Info
Channel: Matt Aspland
Views: 43,961
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, blender, unity, games design, ue5, unreal engine 5, unreal engine 5 tutorial, ue5 tutorial, horror, ai, random, chase, chasing, pawn, detect, detection, sight, see, seen, chased, player, character, enemy, ghost, move, to, location, ai move to, destination, chase the player, ai chase player, stop, find, sensing, noises, noise, pawns, only, players, on, event, ue5 chase, ue5 ai
Id: tKrBdxm4uxI
Channel Id: undefined
Length: 7min 33sec (453 seconds)
Published: Mon Jun 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.