UE5 Tutorial - Weapon Pickup and Drop

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial I'm going to show you how to do a pickup for a weapon um so this is going to be something that's um in the world and then you can pick it up and it'll slot into your hand and then you can drop it again so the first thing that we need to do we need to set up what's called a socket so if we go to the easiest way to do it if you're not sure where the skeleton of the 3D meshes for the character if we go to the blueprint for the character and then select the mesh so you can in the viewport just click on the mesh and then double click the little preview of the person in the skeletal mesh on the right you double click that it'll open up this so what we need to do we can see in this uh left menu you can see all of the bonds so what this does if we go to the skeleton tab on the right we can start to move these around so this won't actually do anything once you exit this but this lets you then preview so what bonds do is it lets you move about the mesh so this is what's called a skeletal mesh so static mesh is they don't have bones it can't be fluid or animated um in any way like this but then skeletal meshes have what's called bonds so they can be moved in various different ways so you can have it like that so this is where we add what's called a socket so if we find the left hand so I want a weapon to be slotted into the left hand uh into the right hand sorry I'm left-handed so um so what we want to do find the hand but if you're not sure where to find it you can just search at the top and it'll bring up everything that says hand so we want the right hand and then if you right click this we're going to add a socket So currently it's named this but we just need to get into position first So currently if we were to slot something into the right hand socket that we just made it would be in the wrist so you can see it's in the wrist here so what we need to do we need to move that to a place where it looks sensible and where you would grip something and then we can rotate that a little bit and now it'll be there so it makes sense once we actually put something in the hand but now we have a socket so because we need to know um exactly the name of the socket when we go into code to snap it to the socket I'm going to name this something easily rememberable so I'm going to call it weapon pickup actually weapon socket I'm going to call it weapon socket so now I know that so it's weapon socket exactly we need to remember that exactly so once we've got that socket done we can close this down so we can close all of this down and if we go back to here and then it I'm going to make a blueprint for a weapon so weapon actor VP underscore weapon and there's a few things that we need to add to the viewport so first of all is the weapon so you might have one 3D modeled already but I'm going to just do one as a cylinder so this is going to be like a staff so I'm going to scale that down just something like this there's something that you need to be careful of is in the 3D modeling software if you've got a custom thing make sure that the zero zero zero coordinates is where you want the player to be holding the weapon so if I move this up this will then be at this area here this is where it'll snap to the player so you can see it intersects here on the lines and this is zero zero zero so if you can put your pivot to where it needs to be in the 3D modeling software you'll have a much easier time but this is currently a little staff or baseball bat weapon whatever so what we then need to add so because of the shape of this I know that I want a captual collision so if I had a capsule Collision and then I set this to here scale it to be sort of around the mesh so this Collision the bigger you have it the further away the player can be to then pick up the weapon so for instance you might want it so it's quite a wide thing yeah otherwise you would have to step onto the weapon to be able to pick it up so another thing so there are ways more complex ways to make it have Collision then take off Collision once you pick it up but the easiest thing for now just to have it so it does slap into the hand is click on the cylinder so the cylinder mesh so I'm going to rename this weapon mesh and I'm going to call this capsule player detect just so it's easier for me to reference it so on the weapon mesh I'm going to scroll down until I can see Collision so I'm going to look at Collision presets and set this to no Collision if not when you pick that up it'll start to fling your player around so we want to set that to no Collision so once we've got that we can then start to do something in record so what we need to do player detect on beginner overlap set the reference to the player so we can also have it on end overlap um remove the player reference so what we're going to do for the reactor cast to character so I've cast a third person character so we get a reference so we're making sure it is the character that is walking over the mesh and then as third person character we want to get a player reference so if we promote a variable here so drag off and then the top option should be promote a variable we then have something called a an act of reference but it's specific to the third person character so we want to rename this so I usually like to call it player ref so I'll play a reference but short so we've got a reference to the player now so what we can then do is set the player reference well actually first of all we want the cast so we want to make sure it is the player that's leaving the other lap and then set the player reference to nothing so don't link up the player reference and then it'll set it to nothing so once we've got that we've got the player reference so what we can then do is if we put an interact key in here we can have a button that then picks up so let's see if we've currently got a input so we do so the way to do this press the plus on action mapping so we won't want axis mapping for now but if we do that and I just call it test and then I can press this keyboard icon until it glows up orange and then it's recording what's going to be pressed next so I can press anything so I'm going to press J and it'll set that key to J but I'm just going to remove that because I have one called interact anyway so I'm going to search the name of that hotkey so I'll interact and then this is pressed released when you press that button do something so what we're going to do here is when we press it attach the um attach the weapon to the player but what we need to do is check to see if the player is actually in um the overlap so there's a couple ways we can do it we could set up a Boolean but we've already got a player reference that we need later on so we can test to see if there is a player reference currently active so the way to do that so this is kind of like a Boolean but testing if this variable has something so if we do this if we drag off the play reference then search is valid so this question mark one so not the function one but the question mark one this will then essentially give you what's what looks like a branch so if the player reference is true so if there is something in it and if there's nothing in it so if player reference is valid when you press interact attach this weapon to Something in the player so if we do attach actor to component so there is actor to actor but I'll talk about why it's actor to component so if we go to the player blueprint we go to this view part we can see this parent socket here so this is where we start to snap it to a socket that's in this mesh so this whole blueprint would be an actor but then because we want to reference something specific to the mesh we need to attach it to components so anything that's inside of this blueprint so capsule component Arrow component mesh camera boom follow camera this is all a component each so what we need to do attach this weapon blueprint to a component which would be mesh so what we do there is from this player reference get a reference to the mesh so get mesh so if you've got a custom blueprint this mesh might be called something different but by default it is just mesh so reference mesh and then this will be attached to the parent so the weapon will then become the child of the parent and then the target itself so it'll attach itself to the parent which is mesh so if you remember we put a socket in and we need to remember the exact name because we need to type the name of that socket now so if I put weapon socket so this is now going to attach to the weapon to the mesh on the socket so what we need to do as well we need to change these so it snaps to the hand so location rule snap to Target rotation rule slapped a Target we can leave the scale as it is because we want to keep the scale if we need to scale it a little bit in the world so I've got this here but I need to scale it a little bit up if I do snap to Target it will then revert it back to the default scale so I'm going to in here keep relative on that so if we do class defaults and then Auto receive input put that to player zero so then it can have this interact uh go through on the weapon if I then play this go near to the weapon press e it then attaches to the player okay so what we then need to do is be able to have it where if we press it again it will then drop the weapon so if we take all of this code and then drag it up there we want to do a flip flop but we need to have it after this so because this is the branch we're going to have this so the way that I did that hold alt I have a mouse over the front Arrow the last Arrow or the node in between and then hold alt and click this then uh breaks that so what we need to do check if the player reference is valid if it is whenever we press interact go between picking up the actor and then dropping the actor so if I do a flip flop and then do that and then if I do detach from actor so if we do that this will just detect if it is a child of anything it will just detach it so we don't need to link anything up to this but what we do need to do we need to keep world on this so if not it will like snap it to somewhere near to zero zero zero coordinates so if we keep World it when we drop it it will just put it where it is in the world currently so if I play this pick it up walk around a bit and then press e it'll it'll be floating but it will be dropped so for a basic attaching to the player then detaching from the player we've got that working so that's how you do a weapon where you pick it up touches to the hand and then when you press the interact again it drops it
Info
Channel: Jake Ogden
Views: 7,362
Rating: undefined out of 5
Keywords:
Id: x8IQ6MjkJig
Channel Id: undefined
Length: 12min 50sec (770 seconds)
Published: Wed Dec 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.