How To Attach A Weapon To The Player | Give The Player A Weapon - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another immersion full tutorial in today's video we'll be showing you how to attach a weapon to the player so in my example i'm gonna be using a sword so what you'd want is when you attack so you do an attacking animation you'd want the sword to be in the player's hand and for it to follow through with the animation as well so i'm gonna be showing you how to create this today so let me hit play and show you what i made today so you'll see that as i'm running the sword is in the player's hand and it's following along moving perfectly like so and if i depress my left mouse button which is my attack you'll see it's gonna follow along like that as well so again this is quite basic but a little bit complicated for some beginners so what i'm gonna be going with is just attaching anything really to a socket of the player but in this case it is a sword and it is a weapon which i am attaching to the player so it's always going to be moving about in the socket like so so this is what we're going over and creating today so without further ado let me delete this code and i'll show you how i've done it so the first thing you want to do is obviously import your animation and your weapon mesh which we're going to be using which i've already done so i've got this animation off of mixmo here which i've just retired to the ue4 mannequin and i've also got a sword weapon here from the infinity blades pack which you can download from the epic marketplace as well it's free to use and you can also use all this stuff in your game too there is a lot more in this pack i've just deleted everything which i'm not using just to save up on space but i'll leave a link to that in the description and so once you've done that we're going to want to open up our skeleton for our player so for me that's going to be mannequin character mesh ue4 mannequin skeleton so again open up the skeleton for the player you are currently using or for the character sorry you're currently using open it up straight away and this is where you want to decide where the sword or the weapon is going to be held so i want mine to be held in the player's right hand so i'm going to select the right hand like so it's called hand underscore r but what i want to do is i don't want it to be there because you'll see that's actually on the wrist so i want to create a new socket specifically for this weapon so i'm going to right click on the socket over on the left here under the skeleton tree i'm just going to simply add a socket press f2 or select it press f2 to rename it and i'm going to name this weapon socket like so make sure you keep in mind how you spelled it i'm going to spell it like that spelling spaces capital letters all has got to be the same later on again you can also spell it however you want but just make sure you remember it and once you've done that you can obviously move the socket to be wherever it is that you want and it's going to be parented to the right hand which is what makes it perfect so where if the right hand goes the weapon socket goes wherever the weapon socket goes the weapon goes so that's why it's amazing however you'll see we don't really know where we want this to be because we don't have a reference of where the weapon is going to be so what we can do is right click on the weapon socket and then we can add a preview asset so now we know where the sword is going to be and what it's going to look like mine is named sk blade black knight and i can add it in like so so now we can see what the sword is going to like when it is in that socket position but the player is not going to be enough a post like this are they they're going to have an actual animation so we can do another preview up in the top right we've got preview controller we're going to change that to be you specific animation and i'm going to change this to be my sword shield and slash like so so i can actually see the player attacking like this i'm just going to pause animation and go back to right at the beginning like so just so i can see where it is now i'm going to select my weapon socket again and you can see that it's really out of place it's not in a good position at all sorry i keep pressing space by accident there we go so you can see it's not in a good position at all so let's move and rotate it i'm also going to lower the camera speed so i can really get into the details of where i want to be and really this is where you just change it for how you want so it's probably going to be different for you if you're using a different weapon mesh and a different character mesh again move it and rotate it into the position which looks best for you and you can also toggle off snapping here as well to get more specific is where it's going to go i want to just rotate it around a bit like this as well so again just for the tutorial i'm not going to go too into detail of it but i think something like that is going to look good for me so the sword is now in the place hand with that rotation that position and that's going gonna look great for me i reckon so if i have to deselect it hit space play the animation this is what it's gonna look like with the sword in the player's hand now obviously the sword is quite big so you might wanna scale it down a little bit but again i'm not really gonna bother too much just for the purpose of the tutorial i'm just showing you how it works you can obviously go into more detail and make it look better for you i might raise it up a little bit though so if i were to select the weapon socket move it up like so that looks a little bit better so i think that is going to work perfectly for me for the weapon socket positioning so we can save and close this that is now the socket set up for where the weapon's going to be however if we press play nothing's here because obviously all we did then was add in the socket the sword that was in there was just a preview it's not actually there so we now need to create this to attach it to that socket on the player so let's go to content and what i'm going to do is create a blueprint for my weapon and i would recommend doing this as well it's also just good practice anyway because then you can do all the code for that weapon i.e the damaging inside of that blueprint for the weapon which i'd recommend doing as well so i'm going to right click go to blueprint class create an actor and i'm going to name this sword bp now you may already have this if you do perfect you can just use that one in here i'm going to add a component adding a skeletal mesh and the mesh with this is going to be my weapon which for me is this sword compile save that is all we need to do in there now again you can obviously do more mode and do more stuff in here so maybe add particles or other meshes as well if you wanted but for me this is the basic thing we just need the weapon inside of a blueprint let's close that and open up our character blueprint which for me is the third person character but for you this could be third first whatever you've named it in here we want to go off of event begin play so i'm going to hold down p left click to get it here and if you've already used it you can hold down s left click to get a sequence with then zero go into the code you have now and then one going into the code we're about to do now i haven't used it yet so i don't need that so what we want to do is we want to spawn in the weapon and attach it to the socket very simply we can come up with everything we can play get a spawn actor from class the class is going to be our sword bp or the weapon that we just created and the spawn transform wants to be the socket so we can get the mesh as the mesh is the skeleton the skeleton is where we created the socket drag out for mesh and get socket transform and now this is where i said it's important to remember how you spelled the socket name because it does need to be exactly the same as that's how it's going to search for it so i named mine weapon with a capital w socket no space capital s like so again if you can't remember you can obviously just open up your skeleton and double check it anyway but this is how i've done it leave the transform space as it is and connect the return value into the spawn transform like so and that is now going to spawn the sword in the correct position however this is going to spawn it there however it is going to stay there in the world space is not going to be attached onto the socket and onto the player so we're going to come out return value of the spawn actor and we're going to attach actor to component the target should connect up automatically the parent is going to be the mesh because again that is where our socket is we want to connect it to the player's hand and the socket name is going to again be our weapon socket or whatever you named it so we're now attaching it to that specific socket so it's spawning it in that socket's place and then we're attaching it to the socket as well the location rotation and scale rule we're going to snap to target so again it goes to the correct position it needs to be in and we can keep well simulated bodies ticked let's compile save close that that should now be it done and working perfectly for us so let's hit play and test this out you can see that this is what worked we've got the sword spawned in the player's hand where we wanted it the right hand the socket position we gave it and if we move about it's staying in there moving with the hand as well if i do my attack animation it's moving with it perfectly as well so that works perfectly so i think that'll be it for this video as we've done everything we want to do we're set up so we can spawn in a weapon in the player's hand and attach it to the correct socket which we want so it's in the correct position the correct rotation the correct scale and it's also keeping there as well so it's moving with me as i move as i jump as i attack all that good stuff which we want and need so what we've done is again we've attached a weapon to a socket which this works for any sort of mesh as well so thanks so much for watching i hope you enjoyed and hopefully 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 [Music]
Info
Channel: Matt Aspland
Views: 56,128
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, attach, actor, to, component, mesh, skeleton, socket, sockets, add, preview, weapon, weapons, sword, shield, give, player, the, attach weapon to player, ue4 attach, spawn, gun, fps
Id: mSCPg3d5Kgg
Channel Id: undefined
Length: 9min 6sec (546 seconds)
Published: Mon Dec 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.