How To Make Something Face The Player - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the thumbnail tutorial in today's video is gonna be a very quick one it's just gonna be showing you how to make an ai or an actor or anything in your game constantly face the player so this is quite good for horror games or something if you want something to just be constantly facing a player or if you have like a moon or something you want to always be facing the player this is very good for that so like i say it's going to be a quick one show you what's going to look like so you can see we have our ai over here which for me is just a character and it's just going to be constantly facing the player like so no matter where i go it's just going to constantly do this so this is quite good obviously you can customize this to make it slower to make it so it lags behind a bit anything like that but this is the base concept of what we're going to be creating today so i'll show you how to do this now so the first thing we're going to do is we're going to want to open up our ai or the actor in which you want to do this in so you're going to want to make a blueprint for this so if you don't have one then you're going to want to do that by right clicking in a blueprint class getting a blueprint actor or for me i just duplicated the third person character again whatever you're making put it to a blueprint once you've done that we're going to open it up straight away i'm just going to find some space right click and we're going to get event begin play this is in the event graph of course if you've already used eventbeginplay then just go to it hold on s left click and get a sequence to use multiple lines of code like this attain zero we'll go into the code you have already then one will go into what you're about to do now although i don't need that as i haven't used it yet off of this what we're going to do is we're going to come out of this and cast two and this is going to be our character so for me this is the third person character but for you this could be third first what if you've named it but basically we're going to be casting to whatever we want the object to constantly face so for me i'm going to then do get player character as the object wild card like so out of this i'm going to answer button character right click that promote a variable and call this character reference so we can easily use and access this later on then i'm going to come down under here find some more space right click and get a custom event so add custom event like so and i'm going to call this one face player so obviously as it sounds this is going to be what's making the object or the aiv the thing we want face our player and we're going to be doing this by using lookout rotations so we're going to do is we're going to right click i'm going to get actor location so the location of this object that we're in now then we're going to drag and drop a reference to our character ref here so get character ref out of this we're going to then also get actor location so we're getting the location of the object and of our player so now this i'm going to do is i'm going to come out the return value of just the ai or out of the object's location and i'm going to find look at rotation with the object being the start and the player being the target so we're going to get the rotation between the object and the player the return value of this what i'm going to do is i'm going to get an r in terp 2 with this return value going to the current the target sorry and the current is going to be get actor rotation so what this is going to do is it's going to get the rotation between the object and the player and then it's going to get that rotation and basically gradually and smooth the transition between the current objects rotation and that desired rotation that we have between that meaning this is what's going to move us from the current rotation and the rotation we want to look at the player so this is essentially like a timeline that we have other animations like opening doors and stuff but this is just more smooth for this rotation now for this return value here what i want to do is you want to set actor rotation like so and this is what's going to actually then move the actor and the object's rotation to then face to player just connect that to this custom event there like so and actually if there's alt left click on that and then before we do this we'll right click the new rotation split structure pin right click return value split structure pin and we'll just connect the z value like so and what this is doing is we only want to rotate it on the zed so if the player goes above the object it's not going to flip upside down obviously if you want it to do that you can just connect all of them but if you only want it to rotate on the z axis which is obviously like this you put it only on the z otherwise it would be doing this as well which obviously we don't want if you do you can leave it like so but i'm just doing it on this edge like that and then on this r sub 2 here we have the delta time in inter of speed the delta time we're just going to drag out that and get world delta seconds and this is essentially like the axi scale on an axis mapping so what it's doing is it's basically making sure that this speed is always relative to the frames the player is getting so if the player is running on 30 frames or the player is running on 60 frames it will still look the exact same and still work smoothly then the inter speed is basically how fast you want it to move so the lower the value the slower it be the higher the value the faster it will be so i'm going to set this to be 20 as that is what i had at the start of the video so i found that to be quite good but you can set this to be whatever you like so if you want to be a bit slower have it lag behind a bit you set this to something lower like five or ten or anything like that but i think that's going to be good for me and then the final step is just out of this set actor rotation here we're going to hold down d left click to get a delay plugging that in there like so i'm going to set this duration to be 0.001 so a very very short duration of this delay that the human eye won't notice of the completed of this we're going to call function face player and the reason we've got the delay there is just so that this code has enough time to finish executing before we call it again otherwise we'll get an infinite loop detected which is why it's just a very very short duration and now this should be done so i'm going to select all this hit c to comment it and then i'll just call this face player or always face player like so compile and save that and now we need to call this function as well so what i'm going to do so i'm just going to come off of event begin play call function face player now obviously you don't need to do this all the time if you don't want you can maybe put a branch here to see if you want to be calling it and that would just be a boolean if you want to rotate it obviously set it to true and false whenever you want to be rotating the actor or not so actually i'll show you how to do that as well just in case you want as you might not understand fully so i'm going to get rid of that there and what i'm going to do instead is hold down b left click to get a branch plugging that into the custom event true will go into the set act rotation there the condition of this i'm going to right click promote to variable and just call should rotate question mark like so compile and change the default value to be false so it's not going to do it by default so then if it's true it will do it if it's false it won't do it and that will then work perfectly like so so now we just want to be able to set this so i'm just going to set this off okay keyboard event like so obviously you can do this if you in a certain radius of it or anything like that or if you're in a certain area for box collisions you can do this customize this however you want just essentially set a boolean to be true so you know what actually no i'll change this again what we do is i'm going to do it if we are close enough so within a certain radius of the ai or the object it's going to start facing us so i'm going to do is underneath this i'm going to right click get another custom event and i'm going to call this should rotate like so it should rotate event so it isn't too similar to the boolean name now this what i'm going to do is i'm going to get a reference to the character reference again so drag and drop in get character reference out of this i'm going to get actor location so very similar to what we did above and then above this i'll get the axe location again so again the same as what we did earlier but now instead of getting the look at rotation like we did there what we're going to do is we're going to get distance 2. so actually we don't want to do it like that sorry we just want to come out the character reference and get distance 2 like so with that being the other actor not the target target itself to value this we're going to get a less than or equal to float and then you can set this to be the radius you want it to be so i'm going to set this to be 500 which is 500 centimeters so basically if the player is within 500 units of this object it will rotate towards us so i'm going to hold down b left click to get a branch we're plugging that as a condition in there and put that to the should rotate like so in the event like that true what we do is set should rotate to be true false our set should rotate to be false and that will work perfectly with that and then off for false i'm going to do is get the very short delay again of 0.001 and then completed call function should rotate event so it's just going to be constantly checking so if we can't rotate it will just constantly check to see if we can and then off a false of this what i'm going to do so back up here off of the faceplate event or false i'm going to call that function should rotate event so it's only going to be calling this event when we need it it won't be a constant loop we'll only do it when we need to actually be checking so again i'm going to select this hit c to comment it and do check should rotate like so so now what should happen is the object should start facing us if we within a certain radius of it so if i compile save minimize hit play to test this you see that it's not rotating as it's not facing us like so if i walk close enough to it it's also not going to be facing us so let's see why that's doing that i think i know why it's because i didn't actually call the should rotate function so what i need to do is offer event begin play i just need to call function should rotate event and then what we also need to do is call the other function as well so back down here under the check should rotate off of true we're setting it to rotate we then also need to call the function of face player so then it does actually start rotating as well so it knows it can and then we're making it so it actually does so now this should work perfectly for us so if we compile save minimize this hit play to test it again you can see the object is not facing us if we walk close enough to it it will then start facing us like so and we can walk around it like this it will constantly face us if we walk away from it it won't be doing it anymore as we are not close enough to it although it appears to still be doing it so let's see why that didn't work because actually it isn't checking this so what we need to do is just call this off of both of these as well actually so that delay will just be going into both of those so we'll be constantly checking if it should rotate that's my bed and unfortunately we don't need to do that anymore as it's going to be constantly checking and pass save that should fix that bug it's not facing us if we walk close enough it is we walk far enough away and it no longer faces us anymore you see that works perfectly like so now you see it's snapping around like that as it starts facing us that's just because of how high i have the end type speed so what you can do is you can just lower that so let's put that at five now this would be a lot smoother for you again that's just your personal preference of what you want it to be like so you can see this is not as fast anymore and it's a lot smoother with how it does everything so again you can lower this even further if you really wanted to all the way down to one let's see what that one looks like for you that's a much slower which obviously if you're going for a horror vibe that might be much more better for you you want to be nice and slow and creepy and stuff like that so the thing about this video is we've done everything we wanted to do we've made it so an object or an enemy or ai or anything like that or face the player or anything else that you choose depending on if you're close enough to it or basically if a boolean is true so you can set this to be however you want on it will just rotate constantly or anything like that so i've done it on if we're close enough to it again you can customize it to be however you want and you can change the speed and all this good stuff as well it's just going to basically constantly be facing the player so thanks so much for watching i hope you enjoyed and i hope 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: 40,119
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, face, look, at, rotate, rotation, player, ai, enemy, object, horror, scary, creepy, look at, face the, follow, looking at, towards, looking, facing, the, objects, enemies, ue4 face player, ue4 look at player, location, always, constantly, head, socket, relative, constant, on, add, set, update, quick, easy, distance, from
Id: JkvswrXyBcA
Channel Id: undefined
Length: 11min 43sec (703 seconds)
Published: Tue Dec 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.