How To Make The AI Run Away From The Player - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another i mentioned 4 tutorial in today's video i'm going to show you how to make it so that ai is going to run away from the player so previously i've made it so that ai will chase the player in today's video we're going to make it so that ai will run away from the player so maybe the ai is being chased or anything along the design so it doesn't even have to be a humanoid like this it could instead be an animal which you were chasing or anything along those lines so what i'm going to do is just move this and then hit play to show you what we're going to make and the reason i'm moving it is just that it's more centralized just so it looks a little bit better because of how narrow this area is which i'm working in so i'm gonna hit play and show you what i'm gonna make as you see the ai is simply running away from the player whichever direction i go in it's gonna just continually run away from the player like this so you might want to make like a herding game or something like this if you ever played stuff like that you have to hide animals into an area again you can use this to make absolutely anything you like but essentially the ai is just going to always run away from the player as you can see here so this is what we're making today so without further ado let me do this code and i'll show you how i've done it and also one final thing as you can see here it keeps stopping like this this is purely again just because of how big i have this area so it's running into the wall as it's trying to run away from me again that's just because of how small i have this area as you can see here again let's get right into it so the first thing you want to do is you obviously want to open up your ai which you want to run away from the player so mine is simply just named runaway ai here which i'm going to open up and press open full blueprint editor like so i don't have any other code in here at the moment because i'm just using a blank template you can obviously use whatever you like if you don't have an ai what i did was i just duplicated the third person character blueprint and removed all the code and cameras and stuff like that so once we're in here we're going to hold down p and left click to get event begin play or if you've already used it you can hold down s and left click to get a sequence connecting it there like so then zero going to the code you have now and then one going into this code we're about to make and off of event begin play we're going to cast to our character which you want the ai to run away from which for me is the third person character like so object obviously being get player character as you can see there and very simply as third person character we're gonna right click promote it to a variable and i'm gonna name this one character reference like so so we can easily access the player like so i'm going to compile and save that and that's all we need to do on event we can play because we're simply just creating a reference to our character although there is one final thing sorry but we'll come back to that later on all right so what we want to do next is we're going to scroll down underneath this and we're going to right click we're going to add a custom event and i'm going to name this one run away because that makes sense for me i want this custom event to be the code which makes the ai run away from the player so that name is makes sense for me and what we need to do now is find the rotation between the player and the ai so it knows which direction it should run away in because obviously we wanted to run it directly away from the player so to do that it's very simple i'm going to right click and get actor location so this is going to get the current location of the ai i'm going to come out of the retirement there and find look at rotation start being the gatax location and the target is going to be the location for the player so it's going to get the rotation between the ai and the player so very simply we can duplicate the get axe location connecting that into the target now however we don't want to leave it as just get out location because that will again be for the ai so what we're going to do instead is however the location of the player so to do that the target is simply just going to be our character reference there like so now it's going to get the location of the ai and then finally look at rotation from that to the actor location of the player so i hope that makes sense and that's what we're going to do to find the correct rotation in which the ai should be running away from and so again to finalize that out of the return value we're going to get the forward vector because again we want to get the forward vector so it's the right direction but we don't want it to be forward we want it to be backwards so it's running away so the return value we're going to get a vector multiplied by a float and we're going to times it by any value but make sure that is a negative so in my example i'm going to do minus 500. so that is 500 units away from the player it's going to run towards anything lower i found kind of makes it a bit jittery but you can obviously mess about with this to get it perfect for you you can put it higher you can put it lower just customize this to get it working better and perfectly for you and then we're going to come out with the get actual location here and we're going to get a vector plus a vector and that's the actual location of the ai and the bottom value is going to be the multiplication there and what that is doing is it's just helping to keep us going in a straight line so it doesn't veer off track or anything like that because we're just making sure that we're adding the current location to the location it wants to go to to make sure it is in a direct straight line there so if you didn't want that you didn't have to have it but it does just make it look a lot better and we're going to come out of the get actor location once again i'm going to break the vector as you can see here i'm going to double click that to get some root nodes to keep it nice and organized and the reason we're breaking the vector because we want to get the z value of this so it stays on the same height although actually i've just thought we might be able to do this away without doing that so what i'll do is i'll leave it there but i'm not going to use it just for a moment and just test out then we'll come out the vector plus a vector and get another break vector like so and this one we do need because we want to mess about with the x and the y because we don't want to be adding or multiplying the z because that will change the height of where the ai needs to go and it just needs to stay on the floor which is why we had this here so we're going to come out of the x and we're going to get a make vector like so x and y connecting in like so and we're going to have the z as the current location so we will actually connect that in there like so i'm gonna and again i'm gonna double click that to get some rear nodes so again let me just reiterate what we're doing there so sorry if i messed up that explanation a little bit but essentially what we're doing is we're getting the x and y of the new location that ai wants to travel to to set that as the vector but we're keeping the same z as what it currently is so the current length location z value is what is going in there just again to make sure it is not trying to climb or get any lower is staying on the same current path but if it does need to go higher or lower it will do that but it's not going to try to if it doesn't need to so it won't try and go 500 units in the air as this would try and do now this vector location here is the destination that ai wants to move to to run away from the player so we're going to come out of our runaway custom event here and we're going to get ai move 2 like so with the pawn is going to be get a reference to self and the destination is going to be that return value there like so and now this should work perfectly for us but this is only going to do it once and it won't do it at all just yet so on success we need to call function run away also connecting that into on fail like so and then also on event begin play we're going to again call function run away like that and now this should be the code done for us so it's gonna again find the correct rotation from the ai and the player and then get the backwards vector so the forward vector reversed of that location and rotation and then just essentially go in that straight line so it's going to run away from the player's current location so we're going to compile and save and then we're going to hit play to test this out let me make sure we have one that dragged in and we do so we hit play you can see it is running away from the player whichever direction we go in is just going to continually run away from the player as you see here and if you want to make this look a little bit different and a little bit more dynamic what you can do is move this ai move to out a little bit again and we're going to right click and get random reachable point in radius random location will go in destination and the origin is going to be that make vector there instead ranges you can set to whatever you like for example 500. so now what it's going to do is it's not going to necessarily just be line run in a straight line it's going to go in that straight line and then wherever that destination is that it's going to want to run to it's going to get a random point within a certain radius around that location so it's going to run away from the player but also be slightly random as well just to again make it look a little bit nice for you if you wanted that let's hit play and test this out as well you can see it is running away from us but it's not just directing a straight line it's kind of serpentine zigzagging away because it's going in a random place so that might be better for you as well if you maybe wanted a more realistic dynamic ai that's running away and maybe a first-person shooter or a better way out like that because it's going to be kind of dodging bullets as you can see here running away but with random locations each time as well so i think that way for this video as we've done everything we wanted to do what we've done is we've set it up so that ai is going to run away from the player in a kind of straight line and we can have it just in a straight line or with random locations as well so it looks a little bit more dynamic and a little bit more realistic as well so again it's kind of maybe dodging something if you're shooting at it or anything along those lines but as you can see it's just going to continually run away from wherever the player is so thanks so much for watching i hope you enjoyed and i 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 in the next one
Info
Channel: Matt Aspland
Views: 25,260
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, enemy, ue4 ai, ue5 ai, run away, run, away, dodge, doge, bullets, bullet, serpentine, runs, hide, from, the, player, direction, opposite, dynamic, move, to, attacked, damaged, shot, at
Id: DEAZ6ItLlB0
Channel Id: undefined
Length: 9min 35sec (575 seconds)
Published: Fri Jun 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.