How To Make Something Always Face The Player In Unreal Engine 5 (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another Unreal Engine 5 tutorial so in today's video we're going to be going over is how to create an object or anything that will just constantly face the player so let me hit play and show you what we're going to make today so if I were to walk over here you can see we have a mannequin not facing us if I get close enough to it it is going to face us and it will now constantly faces as well as long as we are within a certain radius if I leave that radius image over here it's no longer going to be facing us and that is just to help with efficiency and I've got it quite close and you can obviously increase this radius as much as you like and this also obviously doesn't have to be a mannequin the reason I've just done this is because you commonly use this in horror games if you want something creepily following you and you can modify the speed as well which is rotating so it doesn't have to be this quick all of this is very very easy to customize and get perfect for how you want so this is what we're going over and creating today so without further Ado let's delete this code and I'll show you how I've done it so the first thing we want to do is we want to actually just create the blueprint which we want to constantly face us so if you already have this set up perfect open that if you don't we're going to create one so I'm going to right click go to blueprint class create an actor and I'm just going to name this one BP underscore mannequin as that is what I'm rotating in here I'm simply just going to add in a skeletal mesh to add in the mannequin which I want so add in then Manny here and then I'm also going to add in a sphere Collision so we'll add that in like so and this is just going to again be the radius in which this is then going to be active so I'm going to set this to a value of 20 to start with again set this to whatever you like so whenever the player is within this sphere the object is going to face them now you can set this up so it will constantly do it throughout the whole game but I wouldn't recommend that as that's obviously not very efficient so if you want it to be far away I just increase this radius to have it to your desired effect once you've got that set up we're going to go over to the event graph we can delete these three nodes right click on the sphere collection add event add-on component begin overlap so when the player enters this sphere Collision now to test to see if it is the player up to the other actor we're going to cast to character and we want character not the name of your character because this means that any character that overlaps this it will then work for them so that's especially useful if you have a multiplayer game however you will obviously need to set up replication if you're using multiplayer but I'm not going to go into that today after this we're going to right click add character and promote to variable naming this character reference and then out of this just the execution what we're going to do is set timer by event like so the time for this I'm going to set 0.01 but again set that to whatever you like the lower the value the quicker it will rotate the higher the value the slower it will rotate but we also have more control over the rotation speed later on this is more just for how jittery it would look then we want to tick looping as well as we obviously want this to Loop so it's constantly going to faces and then we're going to right click the return value promoted to a variable and just name this timer handle as this will allow us to end this later on so when we leave the radius out of event on the set timer by event we're going to add a custom event naming this rotate and then out of this is where we're actually going to do the rotation code so we're then going to set actor rotation and we just want actor rotation not world or relative just set actor rotation then out of new rotation we're going to get R into like so and that is going to rotate between two different values nice and smoothly so the current is going to be our current rotation so we want get actor rotation and the target is going to be facing us because this is obviously the blueprint of the mannequin not the player so we want the target to be facing the player so how do we do that well we can right click and get actor location not rotation we want the location of this actor then we're going to get a reference to the character reference and out of this also get actor location so now we have the location of both the player and the mannequin then out of get act location for the mannequin or just this blueprint we want to do find look at rotation with the start again being the blueprint the target being the player so we're now going to find the rotation in which the blueprint should be using to look at the player and that return value is going to go into the target of the r interrupt 2 there perfectly like so then the Delta time we want to have the get World Delta seconds and that's just the time in between frames just so it's going to be nice and smooth no matter what your frame rate is and the interrupt speed is again where we can modify the speed I'm going to set it to a value of one but you can have this as low or as high as you like the higher the number the quicker it will be then what we're also going to do is now disconnect return value to new Rotation by holding alt and left clicking then we're going to right click and split the structure pins and just connect in The Zed now you can connect in all of them if you want but I found for me that just having the Zed Works a little bit better but again that might be different for you depending on the object you're using but for me it's a mannequin so Z is the best one then this is now going to constantly rotate towards the player whenever we enter this sphere Collision however we also wanted to stop rotating when we exit the sphere Collision so to do that we can right click on the sphere add event add-on component end overlap and then we're just going to get a reference to the timer handle out of this we will clear and invalidate timer by handle and this will now stop it whenever we exit the sphere Collision as well so compile save and close that place it into a level and then we can test this out so you can see the moment it's facing that way there and this has just reminding me actually one thing I need to do for this blueprint is I need to rotate this by default because for some reason that the mannequins aren't facing forwards by default when they've been created so if you add in an arrow we can see the forward is actually that way so I'm just going to rotate the mesh to face that way you probably won't need to do that unless you're using a mannequin like this as well so I just double check by adding in an arrow there like so and because we are rotating the actor not the mesh you do need to make sure this is facing forwards so now you can see it's facing that way like so if we get in we can see if we go up to it it's going to face us no matter which way we are going which direction we're in it's always going to face us and then if we were to leave this area it's no longer going to do that until we once again re-enter the area like so so I think that'll be it for this video as we've done everything we want to do what we've done is we've set up a blueprint which is constantly going to face towards the player as you can see here and again it's very easy to customize the speed the rotation the location the radius in which it happens and all of that good stuff very easy to customize and I think this is great for a horror game or any kind of game you want really this could also just be for an NPC that you that's talking to player and you want them to constantly be looking at and facing the player so thanks so much for watching this video I hope you enjoyed it and I hope found it helpful and if you did please do make sure to like And subscribe down below so thanks so much for watching and I'll see you in the next one [Music]
Info
Channel: Matt Aspland
Views: 11,526
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, ue5 face player, ue5 look at player, location, always, constantly, head, socket, relative, constant, on, add, set, update, quick, easy, distance, npc
Id: FvOSdkhQ6ag
Channel Id: undefined
Length: 7min 33sec (453 seconds)
Published: Mon Jun 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.