AI Sight Detection And Chase With Music - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another unrelention 4 tutorial in today's video i'm going to show you how to create an ai which can detect you on site and then chase you with chase music and once it stops seeing you and stops chasing you the music will fade out so i'll show you what this is going to look like so i hit play you see the enemy is over there once it sees us it starts chasing us with the music and if we can hide so up here we get out of sight he can't see us anymore the music is fading out and he is no longer chasing us so as you can see if we go down here behind him he can't see us so he's not chasing us anymore so i'll show you how to do this now oh and also you can reset this so it happens again next time he sees you the exact same thing will happen like so so i'll show you how to do that now so let me just delete everything i have and we'll get started so the first thing we're going to do is we want to create our ai blueprint so an easy way of doing this is just duplicating our character so i right click on it duplicate i'm just going to call this ai you can name this whatever you like and we'll just open that up straight away and so then what we're going to do is find some space and then go straight over to the viewport over here and how we want to be able to detect site and this also works with sound as well it's going to add component up in the top left here and we want to get pawn sensing like so and then we can just get that like this you may need to close it and reopen it or just compile sorry then we can now see all of this so this is the pawn sensing component so this green circle here is the sight radius so this is how far it can see so i'm just going to put this down to something like 1340. it doesn't matter too much you can actually just place it in your world and see how big this is so i think that'll be good for me this doesn't matter too much for me and also you can change the hearing threshold all of this good stuff the sensing interval so this just means every half second it will check to see if it can see the player or can hear the player stuff like that obviously enable sensing updates only sends players see pawns hear noises all of that good stuff and the peripheral vision angle which is your peripheral vision is what you can see out the corner of your eyes without looking there so 90 degrees it's basically it can see full 180 in front of it as you can see with this cycle here so customize that for however you like but i think this will be good for me and so then what i'm going to do is as i want to stream on site i'm going to get the on c pawn button there if i just click the plus i'll get an event like so again i'll just put this down in some space down here it's now out of the pawn objects reference here what i want to do is cast to our character so i have the third person character but it could be first third what if you've named it for you the reason we're casting to this is to make sure that it is only doing it when it sees our third person character or basically sees our player nothing else so then out of this we want to get a sequence so you can hold down s and left click to get a sequence like so and then we want to create a boolean variable so down the bottom left hit the plus variable i'm going to call this c enemy or can see enemy i'll do question mark like so and again make sure it's a boolean set to false by default and out of then zero we're going to do is set this to true so obviously when it sees the player it will set this to true so take that like so and then out of then one we want to get a re-triggerable delay and then again set the boolean but now back to false and then re-trigger reasonable delay we want this to be 0.1 seconds above the pawn sensing interval like so so this is 0.5 so i want this to be 0.6 now a retriggable delay is essentially what it sounds so you can re-trigger this delay so if it's gone 0.4 seconds through and then it needs to be triggered again it will do that so then reset back to zero and go all the way through point six again and the reason we've got this is so that basically this is how we can detect when it can't see the enemy anymore so it will automatically stop chasing the player when it doesn't see the enemy but we also need to create a boolean so we know when it can't so essentially every time it sees the player after 0.6 seconds so after the next sense it will set this back to false so obviously because after they sense if it then senses the player again it will set it to true and reset this back to zero seconds and start counting up again i hope that makes sense now what we want to do next is get our ai move too so this is how we actually move the ai so if we just drag out of that set up off of the zero and get an ai move to like so we pawn we're going to drag out and get a reference to self so get a reference to self like so and the target actor is going to be our third person character so of our cast we can do add star by some character drag that to the target actor there and that is basically just going to move it so now out of this what we want to do is get a do wants node and this is where we're going to play our music so if we hold down o and left click we can get a do wants node like this meaning obviously this will only play the song once so if it detects the player again while it's already chasing it won't restart the song or play another one it will stay like this and then to actually play the song we want to do is create the audio component so we'll find some more space and we'll get event begin play like so and here we just want to drag in our audio so i have already imported an audio in here if you get your own make sure it's a wav 16 bit but i've got one off of freesound.org which i'll link in the description below as this is free to use in your own games so it sounds like this but obviously you also heard at the start of the video as well so i'll just drag and drop that in there so it should have add audio component like that make sure to just plug that in then out of the return value what we're going to do is drag out of this and just get a stop so it doesn't play it it just gets a reference to it and we'll right click the return value again promote a variable and i'll just call this audio whatever you like so we're basically creating a reference to this but we're not playing it so like so and i'll select that hit c to make a comment and i'll just do create audio reference like so and we'll go back down here out of the completed of the do once we'll drag and drop our audio reference in there so get and then we'll drag out that and we're just going to play so play like so completed plug it in there and the start time you can pick whatever you like but for me i'm just going to leave this at zero seconds so it starts at the very start of it and now we want to create a function to stop the music and also reset this do once so we can restart the music when we want so to do that what we're going to do is come under this pawn sensing here i'm going to right click and get an event tick node like so we'll drag out of the event tick and we're just going to get a branch so actually i'll hold down b left click and get a branch like so plug that into the event tick like that and the condition of this is going to be our can see enemy boolean that we made earlier so obviously it will set it here and then we're going to come out of false so hold down d to get a delay plug that into false like that and i'll put the delay as 0.4 seconds so it's just under the pawn sensing interval and now they're completed i'll hold down b left click and get another branch with the same condition of can see enemy like so i'll tell you why i'm doing that in a minute and then out of false what we're going to do is get another reference. audio so drag and drop that in get audio drag out of this and we're just going to get a fade out like so and again plug that into the false like that you fade out duration i want this to last five seconds and the fade volume level i want this to go to zero so it completely stops it and then we'll just drag out of this fade out and go into the reset of the do once note there i'll double click to get a reroute like that so this basically means when it isn't chasing the player anymore it will fade out the music and stop it and then reset this do once so we can then play the music again when it starts chasing it so this is it done so i'll walk you through what the code does so when this ai detects the character so when it sees him what it's going to do is set a boolean of cancer enemy to true so we know that it can see him and then it will be moving the ai to our player so we'll be constantly chasing him and then while it's doing that it's going to be playing our music and with this delay here if it stops chasing the player what it's going to do is set can see enemy to zero so what it's going to do is stop chasing the player as it can't see the enemy anymore and again this is so that we know what it's doing and down here if we can't see the enemy so if the ai can't see us or can't see the character it will come up that into a delay and then it will checked again so essentially what we're doing is if it can't see the enemy and then after a certain amount of time it still can't see the enemy which is why we have the double branch here this is just double checking it will then fade out the audio so that the music stops and reset this so we can restart the audio again so if i compile that and save that should be that done so what we can do is i've already done it so we've dragged in our ai there now one final step to what we need to do is we need to get our mouth mesh bounds so if we go into volumes here and we'll just scroll down till we find nav mesh bounds or you can search for it and we'll just scale this up to the size we want it to be so essentially what this is is this is the place that the ai can actually move in so i just want this to cover the whole map like so and if you hit p then everywhere in green the ai can move so this is again we want this to be everywhere that we want the ai to go to so that works good for me and also if you want your music to loop you can open it up and just tick looping under sound here like so and therefore it will loop for as long as you want it to so again if i save close that this should be done so we can test out now if we hit play the player can't see us the ai can't say oh sorry if we walk close to it it detects us the music has started if we run up here and we hide it's quite the enemy the ai sorry should not be able to see us anymore the audio has faded out and it stopped so if i then walk all the way around it hopefully try and miss it and find it quarters so as you can see it found us so it saw us it started chasing us the music started again we are now hiding it can no longer see us the audio is faded out and it stopped chasing us so if i now try and find it you see it can't see us as that as the sight radius i've given it as it can't see us it's not chasing us the audio isn't playing if i go close enough it can see me it's chasing me and it is now also playing the music so it is continually chasing me like so i'm just going to stop as the music is quite loud but i think that'll be for this video in the next video i'll show you how to create it so the ai just randomly roams about around here and then still chases the player when he sees him but again that's the next episode so i think that'd be for this video as we've done if we want to do we've created our ai and we've made it so that it will chase the player when it sees us and it will then play some music as well while it's chasing us and when it stops chasing us it will fade out that music and then allow us to redo this again so thanks so much for watching i hope you enjoyed and i hope that helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one [Music]
Info
Channel: Matt Aspland
Views: 69,362
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to, games design, ue5, unreal engine 5, horror, ai, random, chase, music, sound, audio, play, while, when, chasing, pawn, detect, detection, sight, see, seen, chased, player, cast to, character, scary, enemy, ghost, fade, in, out, move, to, location, ai move to, destination, chase the player, ai chase player, stop, find, hear, hearing, heard, sense, sensing, interval, threshold, updates, noises, noise, pawns, only, players, on, event
Id: O88PwLAYAoc
Channel Id: undefined
Length: 10min 51sec (651 seconds)
Published: Wed Oct 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.