How To Make The AI Head Follow The Player | Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another invention 4 tutorial in today's video i'm going to be going over showing you how to create it so the ai head is going to follow the player so previously i've made a video where the whole ai is going to rotate to follow the player but some people are asking to make it a bit more creepy and just have the head follow the player so that's what i'm going to do today let me hit play and show you what we're going to make so if i want to walk up to this ai you can see that the head is actually following me wherever i walk so if i go over here the head is going to rotate this way and go over here it's also going to rotate to follow me this way now it's not massively noticeable but you can tell it mainly when you are playing the game and of course you can make it so it moves further to the right and left so if i'm here you can make it so it's facing the player completely but i limited it just a little bit just to keep it looking a bit more realistic but in the actual tutorial i might show it further along just so it is a bit more noticeable again this is what we're making today so without further ado let me show you this code and i'll show you how i've made them so the first thing i want to do is i'm going to create an aim offset so to do that we're going to right click go to animation and then create an aim offset 1d and this is going to be for the skeleton which your ai uses which for me is just the ue4 mannequin skeleton i'm going to name this ao for aim offset head rotation does that make sense for me that's what it is is an aim offset for the head rotation of the ai i'm going to open it up straight away in here i'm going to change the horizontal axis name to be the your because we want to be changing the head from going left to right which is the z-axis which is the your the minimum value is going to be minus 180 with the maximum value being 180 and the number of grid divisions is going to be three so now you can see we have this little chart and this graph down here going from 180 to 180 with three divisions in between like so we have three grids here and the last thing we're going to do in here for the moment is set up the preview bass pose to just be our idle animation for the ai which for me is the third person idle there i'm going to save and minimize that and we'll come back to that later on what we need to do now is set up the different animations to input into here so we obviously want straightforward left and right so to do that i'm going to get my idle animation for this ai which for me is going to be content mannequin animations third person idle but again use whichever animation you have for your ai so i'm going to select that hit control c go back to my folder and hit ctrl v and just simply rename this to ao for aim offset forward as that's what this is is going to be the forward facing animation and i'm going to open it up straight away now as you can see in here we have all these frames for it to play an idle animation we don't want that we just want it to be one single frame so let's move the timeline all the way back to the beginning right click on it and remove frame 1779 and do this again all the way until you remove all of the frames and you're just left with one frame like so so when you hit play it's not moving at all we just have this essential pose asset for our id animation and in this way we can now create a new animation for moving the head so again it's just one single frame nothing's moving like so what we're going to do is we're going to change the additive settings to be from no additive to mesh space and the base post type from skeleton reference to selected animation frame and the selected animation is going to be our idle animation once again not the ao forward but just the third person idle which we are adapting this from and we can save that that's all we need to do in here because like i say this is the forward facing one so i don't need to change it as it's already facing forward we just need to make sure it's one frame with the additive settings in here we'll save and close that right click on it and we're going to duplicate it like so naming this one ao left so we've done the forward now we're doing the left and because we've duplicated that we already have a set up to be just one frame with the additive settings in here all we need to do is just rotate and move the head so it's facing left now obviously this is for the ai's left so that's going to be our right so it's facing this way because if you're facing the ai like this left is that way so what i'm going to do is select the head go to the skeleton tree and then make sure i'm selecting the neck you can rotate the head if you want obviously rotating the neck will just look a little bit more realistic so again this is left so i'm going to rotate it this way and i will rotate it a little bit further like this just to make it a little bit more creepy and a bit more noticeable for you to see as well for the purpose of the tutorial again obviously move this however far you want it to go so however far you want to rotate if you want you can even do another animation for going all the way backwards as well but i'm not going to bother with that i'm going to have it facing this way so it's a little bit more realistic now with the next still selected we're going to hit plus key at the top and then apply and hit save and now we've saved this as a new animation for facing to the right or left sorry like that close this duplicate the forward animation one final time and this one is going to be ao right so we've done forward left and right and this is gonna be the same thing once again select the neck and just rotate it to the right however far you want it to go and again i think that is gonna be good for me hit key hit apply and save so i've done that one quite quickly because again it is very simple all we need to do is rotate the head because again we've already set up the frame and the additive settings and all that good stuff we're just creating these new animations for making the head rotate to the left and right and obviously straight forward as well close this and open up our aim offset for the head rotation once again and we're going to input these animations in here so what i'm going to do is over all the way on the left on minus 180 is going to be ao forward then the one to the right of that is going to be ao right and then after that ao left after that ao forward once again so now when we're at minus 180 or 180 we're going to be facing forwards as you can see there and if we're about 60 we're gonna be going right and about 60 is left so as you can see as we move beyond the ai like this the head is going to change rotation and again i have forward on the outermost parts like this because when we're facing ai this is actually the division line so if you don't have it like that it's just going to snap between the two so this is how i have it set up it may be slightly different for you but obviously just mess about with it to get it perfect but it should look something along these lines so we can hit save and now this is the aim offset setup so we've made it so we have all the different horizontal axis settings which we want and we've inputted the animations in here after creating them as well so let's close this and now we need to actually use the aim offset itself so to do that we need to go into the animation blueprint now if you don't already have a separate animation blueprint for the ai we're going to want to make one but if you'd already have a separate one great use that but i don't so what i'm going to do is just duplicate the one which it's using at the moment because at the moment it's sharing one with the third person character so again for me that's mannequin animations third person and vp copy that and paste it because again it's sharing one i don't want it to share one i want it to have its own separate animation blueprint i'm going to rename this to ai and in bp it doesn't matter too much because again this is just for a tutorial but for you you'll probably want to give it a proper name but that might work for you as well just make sure that ai has its own separate animation blueprint from other ai and from the character and all that good stuff i'm going to open this up straight away now everything in here is going to be the exact same as the character but it has its own separate one now so it isn't sharing and to make sure he is using this we need to open up our ai which i already have set up here select the mesh and then we're just going to change it from third person and bp to ai and mvp here so that was using that animation blueprint we can compile save close that again and go back to the animation blueprint here now what we need to do in here is in the event graph we're going to set up a reference to our character blueprint so we're going to right click and get event begin play so event blueprint to begin play now of this we're going to cast to our character which for me is the third person character but for you this could be third first or whatever you've named it and the object is obviously going to be get player character as this is for our player character and as third person character we're going to right click promote a variable naming this character reference like so because again this just it gives us nice easy and efficient access to this character blueprint so we can find out where it is in relation to the ai so once you've set up that reference there we're gonna go to the end of the code which we already have which is off update animation go to the end of that drag out the character reference here get character reference right click on it and we're going to convert to validated get this so this essentially does the same thing as an is valid node so it's only going to fire off this is valid once this has been set because initially this will fire off before they begin play so we'll try to use it before it's been set so this will just alleviate any errors which we might get like so so i hope that makes sense and out of the character reference which we have here we're going to get actor location like so getting to location and then underneath this we're going to right click and try get pawn owner so that's going to then get a reference to the ai and return value we're going to get act location so we're getting the location of both the ai and the player character as well then out of the try get pawn owner get out location we're going to find look at rotation so the start wants to be the ai's location and the target wants to be the player's location because again we want to find out where the player is in relation to the ai so we're going to start at the ai's location and try and find the player's location and that will then give us the rotation between those what will then obviously help is find out which direction of the ai head needs to face in order to face the player and so we're going to right click the return value and split the stretch pin because obviously we only want to be messing about with the yaw or the z value which we set up in the aim offset earlier so we're going to right click on the yaw promote it to a variable simply naming this your and we're going to connect that into the is valid there so once we can access the character blueprint what we're going to do is get the location of it and get the location of the ai and find out the rotation between that so where the player is in relation and context to the ai and then we're simply just going to get the your or the z value for that rotation so we know which direction the head needs to face we'll compile and save that and we're going to go to our anim graph the state machine and into our idle state here so again this is the idle state for me which is idle run because this is set up for me already i'm going to move this out and then drag in my aim offset head rotation here connecting that into the output pose and the base pose can still be this blend space for the idle and walking like that alpha we can leave as one and the your is going to be our your variable which we've just set up here so compile save and this should now be the code working perfectly for us so we can hit play and test this out so you can see the ai is kind of already looking at looking at us as you can see there because of where we are and we need to get closer to it so we're going further this way you can see the head is rotating to faces like so and as we move around the head is following us like this and if you move like this it does slowly rotate all the way around as well it doesn't snap so this is working perfectly for us so i think that'll be it for this video as we've done everything we want to do today we've set it up so that ai's head is going to slowly rotate and follow the player wherever we go and i say slowly it's only so because of how slow i'm moving but the ai's head is going to follow and rotate to follow the player as you can see here and smoothly does it as well so again this is a nice little creepy thing which you might want to add into your game it doesn't even have to be on a character like this it can be on let's say an actual mannequin or a doll or anything like that or it doesn't even have to be horror related either it could just be a general npc but again this is what we set up today 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 you in the next one
Info
Channel: Matt Aspland
Views: 20,667
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, head, follow, rotate, player, ai, the, rotation, look at, horror, creepy, ai head, rotates, follows, the player, character, body, look, at, looks, location, direction, smooth, smoothly, to, aim, offset, aim offset, neck, face, animations, animation, anims, anim
Id: qRUuu-ZMvCU
Channel Id: undefined
Length: 12min 7sec (727 seconds)
Published: Fri Aug 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.