How To Pick Up And Drop Items | Hold Items In Hand - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another imagine 4 tutorial in today's video we're going to be going over picking up and dropping items and having them placed in the player's hand as you can see here so in today's video i'm going to be using an example of a sword which we set up in a previous video and you don't necessarily have to watch that although i would recommend it because it will really help you understand some of the basic concepts we're going to be going over today so again previously what we did was just give the player a socket in which we can attach a weapon or an item to so they are holding it in their hand and it moves with them as you can see here and what we're going to be doing today is setting up so we can now pick up and drop any item we want so if i go over to the sword on the ground i press f and that's my pick up button we're going to pick it up there's no animation but it does get picked up and placed into the player's hand attached perfectly like so and if i press g or my drop button we're just going to place it on the floor like this so this is what we're going over and setting up 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 open up our items which we want to be able to pick up so this can be all of the ones you want but for me it's just my sword bp we have here in the viewport we want to make sure that we do have a sphere collision or bots collision or anything really which allows the player to actually be able to interact with it i.e they're going to be overlapping it which is why i've used a sphere collision here what we also want to do is select sword bp self or just the self item of your actor up here and in the search details we're going to go for tags add a tag i'm going to name this one pick up a bull like so there's probably a better word for it but i really can't think of what it is now i'm pretty sure there is but i was like say my mind's gone blank so pickupable works for me i'm gonna give it a tag like that and we're doing this so that we can differentiate between different items we want to be able to pick up and that's so that we can't walk up to a door accidentally press f and pick up the door we don't want to do that we only want to be able to pick up the items which we want for example this sword or a mug or a map or a torch anything which you are using in your game make sure to give it this tag spelt exactly the same each time once you've done all that we can compile save and close this and that's all we need to do in our item bp again make sure to do that with all of the items you want to be able to pick up now we're going to open up our character blueprint which for me is the third person bp blueprints third-party character but again for you this could be third first whatever you've named it now in here we're gonna go to edit project settings go down to input and add in some action mappings you see i've already got them but i will delete them and add them in again so we're gonna hit the plus ax mapping to add one naming this pick up and give this the key you want for me that's gonna be f as for me e is normally interact f is normally pick up or flashlight obviously you could have interact and pick up it's the same button if you want it doesn't matter but for me i want them separate like this then we're going to add another x mapping and this one is going to be for drop obviously you can do pick up as well so if you're not holding anything f pick something up if you are holding something f drops it but for me i want separate buttons so i want g for drop like this once you've got these action mappings set up we're going to close this right click and search for the first action mapping we made called pick up like so so now whenever the player presses the button you just assigned it's going to fire off this event here what we want to do in this event is first off see if we are already holding something because if we are we don't want to try and pick something else up so what we want to do is create a new variable keeping it as a boolean so it's a true or false value and naming it is holding question mark like so then we're going to hold ctrl drag this in to get it and put this to a branch by holding down b left click to get branch connecting it into the pressed and the condition into is holding like so so again the first thing we are doing when we want to pick something up is seeing if we are already holding something now if we are we don't want to do anything or for you this could be where you do your dropping code which i'll show you later on but for me i don't so i'm going to come off a false so if we're not holding anything then we can pick something up so what i want to do is see if there's something in the area around us for us to be able to actually pick up so i'm going to right click and get overlapping actors which is why i did the sphere collision earlier on and the class filter is just going to be acta like this now this part of the code might look similar to something i've set up before for interacting using blueprint interfaces it's very similar this is the most efficient way of doing stuff like this i'm not using blueprint interfaces in this though as it's not needed out of overlapping actors we're going to get a full each loop with break so again once we find something to interact with we're going to stop this and that's going to go into the fault of the branch as i mentioned earlier like this then out of array element what we want to do is get an actor has tag and this is why we set up the tag earlier on so this is where we're now checking to see if this specific actor has this tag to know if we can or can't pick it up so for me the tag was pick up a bull or one word with a capital p like this so again make sure you spell it correctly for how you did and for what you set as this is also giving a boolean return value so true or false so we want to input this into a branch by holding down b and left clicking to see if this is true or false and then do our recording code off of it obviously going into the loop body as well there so if this is true that means we can pick it up so what we want to do is then go into the code for doing that false means we can't pick it up so what you could do then maybe is that's where you have your interact code so if you want it off of the same button what you could do is false goes into interact so for example that could be opening a door and true would be picking up an item which might be in front of the door so if you do that this system will always prioritize picking up again i'm not doing the interrupt code i'm just doing picking up so what i want to do is right click on array element promote it to a variable i'm going to name this one held item and this is so we can keep a reference and store the value of the current item we're holding again that's going to true because what happens is anything off of true is this is us picking up the item so we want to keep a reference to the item we are currently holding just so we know which item we want to drop later on when we do that code we're going to come out of held item here and attach actor to component now this is similar to the code we did in the previous video but i'm just going to show you it again anyway but you might already have this the parent is going to be mesh like this and the socket name is going to be the socket which again we created in the previous video which i named mine weapon socket spelt like this but for you this might just be item socket or whole item socket anything like that but for me it's weapon socket just because the example i was giving was for a sword and the location rotation and scale rule want to be snap to target so it's going to go to the current position of the socket so it goes into the correct place it's not going to stay where it is in the level and that is now going to attach that into the place hand so we are holding it we now also just want to update the code by setting is holding and setting it to true just so the code knows we are now holding something and this also wants to go all the way back into the break of the forage loop because again we've now picked up an item so we don't want to continue searching for more items as we've already found one to pick up and that should now work perfectly for picking up items now we also want to drop it now dropping can be quite simple but i'm going to advance it just a little bit just so it does drop onto the floor and be on the floor because i think that's going to look a little bit better for me so underneath this code actually i'll select this press c to comment it and i'm just going to name this one pick up or pick up item actually i'll do like so and underneath this i'm going to do the dropping code so i'm going to right click and get drop the action event we created earlier out of press we once again want to get the is holding and branch just to check if we are already holding something so i just copied and pasted it from up there because again we only want to be able to drop something or try and drop something if we are holding it because if we try to drop something that we are not holding we're going to get an error and probably crash the game or just nothing will happen but we will get an error so false we obviously don't want to do anything because we don't want to drop something we're not holding so we're going to come out true out of true i'm going to get a line trace by channel because again what i want to do is make sure that it does drop onto the floor and doesn't just float in the air so to do that i need to use a line trace to find out where the floor actually is so for the start of this line trace i'm going to get actor location so we're going to draw a line from where the player currently is i'm going to duplicate that drag out this one and get a vector minus a vector leaving the x and y as zero and the z i'm just going to put as 500. so we're going to subtract 500 from the player's current location and that will be the end of the line trace so we're drawing a line from where the player is down 500 units and that should reach the floor if it doesn't obviously just increase the z value in here but it should reach the floor because you shouldn't really be more than that but again obviously you can increase this if you need to now if we come out of out hit we're going to get a brake hit result here you'll see that we have a location here this location is where the floor currently is or basically whatever this hit that's where that is so if you were on top of a wall and you dropped it it'll be placed on the wall if you're on top of a table it'll be placed there basically where if this line trace hits so whatever is underneath the player is where this will be so that's where we want to put the actor so what we want to do as well is i'm going to hold down b left click to get a branch with a condition go into the return value just so that this will only happen if we do actually hit something so again if you're really high up in the air maybe so you're falling this doesn't hit anything it's not going to work it's not going to drop it obviously if you do want to drop it then just again increase this here so you can be really high in the air and still drop it so when we are dropping it we want to get our held item so hold ctrl drag it in to get out of this we're going to just simply detach reactor so now it is no longer attached to the player's hand so we're not holding anymore now we could just do that with none of the line trace but again that's just going to be static in the air we want it to be on the floor so the location rotation and scale rules also want to be keep world so again they are where they currently are in the world then to actually just put this on the floor we're going to drag out of held item again and set actor transform connecting enter there right click new transform and split the structure pin with a new transform location being the location from the breakkit result we spoke about earlier because again i've already explained that rotation and scale you can leave the same if you want but for me i'm just going to put -90 onto the scale because the items that i have in my game want to be rotated like that obviously you can change that rotation to be whatever you need it to be for you i imagine you'll probably just leave it at zero depending on what your items are but for me it's going to be 90. what you can maybe even do is have it set up so depending on what item you're dropping that may change or you could try and do some math to line up with the floor always obviously change it based on what's good for you then again we just need to set is holding to false as we've now dropped the item we're no longer holding it so we can just set it to false or unticked like this and let's compile save and that should now be the code all done working perfectly set up for us i'm again just going to comment this naming it drop item like so so let's compile save and hit play to test this out so if i press f here or g here nothing is happening if i walk up to my sword here press f you see it is now in my hand is attached whatever i do it's following me like this and if i jump and i press g you'll see we've dropped it and it's gone straight to the floor press f i can pick it up again so i can pick up and drop my weapon and when i do drop it it goes onto the floor perfectly like so so i think that'll be it for this video because we've done everything we want to do we set up so we can pick up an item and drop it as well and when we do pick it up is placed in our hand and attached to us perfectly so we are carrying it as we want like this no animation setup for this so obviously you can do that if you want i with maybe animation montages or the state machine if you want specific animations while holding as well but this is what we've done and set up today the basic system of picking up and dropping a weapon attaching and detaching it like so using blueprints as well so thanks so much for watching i hope you enjoyed and 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 [Music] you
Info
Channel: Matt Aspland
Views: 84,978
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, item, items, hold, pick up, move, pick, up, drop, interact, attach, detach, to, mesh, blueprints, with, via, how to pick up, sword, place, down
Id: EbZrfsJ1fWc
Channel Id: undefined
Length: 12min 42sec (762 seconds)
Published: Sun Dec 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.