How To Make A Friendly AI Companion Follow The Player - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another religion 4 tutorial today's just going to be a quick and simple one we're going to be setting up a friendly ai companion so i might do multiple parts of this if you want to add more stuff to it if you do let me know in the comments down below in this one we should be doing a basic follow system so ai is going to follow the player and stay a certain distance away so it's not gonna be right up in his face and also when we sprint the ai will sprint as well so let me show you what it's gonna look like so we can go in you can see the ai's walk to us and they've stopped there and you can change the distance they can stop further away stop closer anything like that and if we start walking away they're going to follow us if i start running they're going to start running as well obviously i haven't set up the different animations because i just wanted them to be a bit faster so this is what we've got so we're running we stop they start walking as well if we stop they stop with us so this is what we're gonna be setting up today it's quite good it's a very simple mechanic that you see in a lot of games especially story games they might follow you around anything like that so this is what we'll be making today so let me delete this code and i'll show you how i've done this so the first step we want to do is we want to create our ai and if you've already created your ai you don't need to do this you can do it in there what i'm going to do is i'm just going to duplicate the third person character or the first person basically your character blueprint so for me that's content third person bp blueprints third person character i'm gonna hit control c after selecting it and go back to my ai follow folder which i have here or just your companion whatever you want and hit control v to paste it i'm gonna select it hit f2 to rename it i'm just going to name it ai companion you can name this absolutely whatever you want and i'm going to open it up straight away once you're in here we can delete all the rest of the code that we have don't worry if you don't have some of this these are from previous tutorials we're going to delete all this code and i'll also delete the other stuff in the viewport which i don't need for example the camera and all that good stuff there because all we need here is just the ai itself and the reason we did it from the character blueprint is so we can include the character movement in here like so so once you've done that we're going to go back to the event graph and we're going to find some empty space right click and get event begin play out of this we're going to cast to our character for me that's the third person character the views could be third first or if you've named it but this is essentially the character that you want the ai to follow so for me that's the third person and the object is obviously going to be get player character as dire passing character we're going to right click promote to variable and just name this character reference so we can use this later on underneath this we're going to right click add a custom event and name this one follow player like so now this we're going to hold down b left click to get a branch plugging that in there like so the condition of this is going to be the distance from the player so we're going to right click get distance 2. leaving the target itself and the other actor is our character reference there so we're going to be getting the distance between the player and the ai now the return value we're going to get a greater than or equal to so float it's greater than or equal to float plugging the condition into the branch there and this is where you set up the distance the ai needs to be from the player in order to stop so if they go below this they're going to stop moving so i'm going to set it as 400 but you can set this to be absolutely whatever you like just mess about the values to get it perfect for you then off of true of this branch so if it is greater than this distance we want the ai to move so we're going to come up true and get an ai move too plugging that in there with the pawn as get a reference to self we target actor as our character reference again there so it's just going to keep moving to the character there i'm going to select all this hit control c control f to duplicate it plugging that one into the false now and now the target actor isn't going to be the character reference it's again going to get a reference to self and so what this one is going to do is it's just going to stop it so if the ai is far enough away it's going to move to the player and if they're close enough it's just going to move to itself ie stop the movement then what we're going to do after this is hold down d left click to get a delay with the duration as 0.1 or 0.2 anything like this is just how quickly it's going to check and we'll plug that into just the execution of the ai move too so not on success or unfail just out of the top one as we want to be doing it constantly actually i'll let the duration to 0.2 and i've completed our call function follow player so this delay is essentially just me checking so we're constantly checking to see how far away the ai is from the player so we can then stop moving it when we need to now we could use box collisions and stuff like that instead so that when the ai enters the box collision it stops however that can then mess up a lot of other stuff later on and confuse it for you ie that will then also increase the player's condition or the ai's collision stuff like that so this way it's just a bit simpler and helps you understand stuff later on and makes it easier for you then so i hope that makes sense again obviously you can use a sphere collision if you'd like but this is the way i'm doing it i think it's slightly more efficient so once we've done that we're gonna go back off event begin play and just call function follow player so then when we start the game it's going to start from the player so what you could do is just have this off a button event instead so then call the function follow player and here have a branch so actually i'll do that as well just in case you don't know what i mean so now we're going to set it up so that you can choose when and when it shouldn't follow the player so in between the custom event and the first branch i'm going to hold down b left click to get another branch plug in the true into that branch false into nothing i'm going to create a variable so hit plus variable there and i'm going to name this one follow player question mark compile and i'll leave it as false by default and that's what we need to do in there i'm going to compile save if we minimize open up our character blueprint again so it's third person bp for me i'm going to do is now just right click and get a one keyboard event or this can be anything you want but i just want to be when i press one it starts following i shall have it as one together on and off so out of the one okay flip flop a is going to be a reference to our character so a is going to be following it so we're to do as well is right click get event begin play if you've already used it you can hold down s left click to get a sequence to fire up multiple lines of code here but i haven't so i don't need that out of this we're going to get all actors of class with the actor class being our ai companion that we have here out actors we'll get a copy leave the index to zero and the output we're going to right click promote a variable and call this companion reference like so so the same way we did a character reference now we're doing a companion reference then back to this flip-flop here we're going to get that companion ref and out of this we're going to set follow player to true plugging in a and taking it so it's true and then what we'll do is i'll duplicate this set plug it into b and then we'll untick it so it's false so we press one we're going to toggle on and off the following of the player from the ai so this will work perfectly so now we're also going to do is set it up so that when we sprint the ai sprints as well so just underneath this code it's still in the character blueprint what we're going to want to do is right click get a left shift event or this comes off of your sprinting code you already have so i'm just going to create a basic sprint code now if you already have it go off of that off of pressed i'm going to get the character movement obviously and set max walk speed again this is just setting up my sprinting first of all duplicate that down there off of released and what i'm going to do is have it so we go to 1200 when we're sprinting and back to the default of 600 when we're not now what i'm going to do is get the companion reference there out of this i'm going to get character movement like so and now this i'm going to set max walk speed again plugging that to the top one here now what i want to do is i want to have it a little bit slower so i'm going to set this to be 1100 so the ai is slightly slower than the player now you don't need to do that but it does help it a little bit it just looks a little bit better especially from the testing i did earlier i found this to be better i'll duplicate this again plug it out there the targets into the character movement the speed as 500. so again it's just a little bit slower compile and save that and this is the code done what i'm going to do is then also change the companion's base moving speed so i can set the character movement the max walk speed i'll set that to be 500 instead of 600 so again it's just slightly slower than the player so if we save compile minimize we can test this out and make sure you've also got a enough mesh volume as well so ai can actually move to do that you go to volumes get enough mesh bounds volume and just scale this up to be the size that you want and a good way to test if it's going to work is if you press p the floor should turn green where the ai can move like so if it's red or not green then it hasn't worked to you you need to make sure that you move it or the collision is correct all that good stuff so let's drag in our companion and test this out so any companion there we hit play this shouldn't follow us until i press one that didn't start following us and there is a good reason for that what we've got to do is actually call the code so if we go back into the character blueprint here we set the boolean to true but we also need to call the function so i'll companion ref again i'm going to call function follow player it's a very simple thing i just forgot to add that in now this should work for us so again we try this hit one it's starting to follow us like so it gets 400 units away from us and stops we move away it's going to follow us if we start sprinting it will also start sprinting as well while still following us again if i stop it will stop if i stop walking away it's following us if i press one it should stop however it didn't or it did sorry but it wanted to finish off the movement so the reason for that is it wanted to finish off where it was going first so another simple way to fix that so it's good that we tested that out is here we just move it so we're going to come off ai companion again or companion reference sorry and get an ai move too i actually know we won't do that we'll go into the companion here right click and add a custom event name this stop movements plugging that into this bottom ai move to there which is the one we set up to stop the movement compile that go back to our character companion ref it will stop movement so call function stop movements there plugging that in so this is why it's always good to test your code because you might forgotten something or miss something else and so now we're fixing it like so very simple things just forgot about them minimize let's test this again press one it's gonna follow us press one it stops following us so that works perfectly press one it's gonna follow us when it reaches us it will stop at the right distance we follow it follows we run it also runs again if i press one it stops following us so this works perfectly again this is just a basic friendly ai companion follow code and if you want more videos like this on a friendly ai companion let me know in the comments down below what you'd like to see and i can hopefully get around to doing them soon so again i think that'll be this video we've done everything we want to do we set it up so we can toggle on and off the ai following us and it will follow us to a certain distance away from us and it will stop and if we walk away it will walk with us if we run it will run with us again we can toggle it on and off like so so thanks so much for watching i hope you enjoyed and i hope you found it helpful if you did make sure to like subscribe down below thanks so much for watching and i'll see in the next one
Info
Channel: Matt Aspland
Views: 34,510
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, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, ai, follow, player, distance, stay, mimic, copy, movements, run, running, sprint, sprinting, jump, code, companion ai, friendly ai, npc, ue4 npc, ue4 ai, story, friendly, companion, ai follow player, always, near, far, away, from, game, blueprints, blueprint, roam, ai move to, move, stop, start, toggle, actions, movement
Id: 0Xbq5BrvmgQ
Channel Id: undefined
Length: 11min 14sec (674 seconds)
Published: Wed Feb 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.