Pickup and drop system with physics on UE5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh today I'm gonna be showing you how to implement this function of pickup and drop so when we press play here as soon as we overlap the Box Collision we can press F and then we can pick it up so when I press F again we can drop simulating the physics so this is the final result and let's go to the tutorial [Music] uh so here we are on our third person template so the first thing we're going to do we're going to import our pistol that I Linked In the description below so you're gonna get this pistol hello so let's click here on import choose your fbx file and then here we're gonna have the settings you don't need to click on anything just go to import all all right so let's put this those assets in a folder let's create a new folder let's call this John all right let's select all of this and then move it to here all right so first thing you gotta check out is the size of the gun so let's drag and drop to the world as you can see our weapon is very big so we can double click on the static mesh and then go here on the details on the search bar you can type for scale uh we're gonna want to set our scale our import uniform scale to 0.05 and Report Mouse report base mesh yeah so you got to check this out another thing other from scale you gotta check the Collision uh make sure it has simple Collision activated so you're good to go so we can delete this already uh so now we're going to create a blueprint for the pistol so let's select the pistol click on right click and then we're gonna go to the asset actions and create a blueprint using this so we're going to select our gun folder and we're gonna rename the blueprint to BP underscore just so yellow and now we have our blueprint here we can drag and drop this pistol to the world and let's go here to the BP pistol yellow let's go to the viewport and here's our pistol so we're gonna add a collision box so only when the character overlaps this box here uh we're gonna be able to pick up the gun so if you are far away from the gun we're not going to be able to pick up so one thing that I like to do here we're gonna uncheck this option hidden in game so that we can see the Collision box in the world like here okay very good so we're gonna go to the static mesh and we're gonna go down here to collisions physics first let's simulate the physics check that option on and The Mask we're gonna choose 40. scroll down a little bit until we find this let's put custom physics only this physics body and then we're gonna check ignore okay so we're gonna check the shield options and those ones too so you're going to do exactly as you do here that's pretty much all you got to do here on the viewport uh let's go to the box Collision we're gonna get the begin overlap so this code here is going to be executed after our character collides with the Box Collision which you know is this box here and we're going to get the end overlap this end overlaps is when our character gets out of the box Collision so when our character does not overlap like he leaves the Box Collision this code is going to be executed so we need a Boolean value we can choose our name I'm going to call this pistol overlap so this Boolean value is going to be either true or false so when our character overlaps this is going to be true and our character leaves the Box Collision it's going to be false and we're going to be able to access these variables here on the third person character you can press save we can go to content third person Blueprints and let's access the third person character let's go to the event graph and here I already created a variable pistol attached you can create it too um so let's go let's type event begin play so we gotta check if the the pistol is in the world so let's get our class all actors of a class so we're gonna choose our BP still yellow get a copy and then promote this copy to a variable and we can name this variable to pistol just simply like that let's name this pistol all right so uh when we press play here our pistol physics is on but when you press F nothing happens because we didn't set up the uh the code for it so when you press f we're going to type here f key when you press f we're going to execute the code so the first thing we're going to do here we're going to check if our pistol is already attached to character and we're going to get the branch so if the pistol is already attached don't do nothing and if the pistol is attached we can drag off pistol and say get pistol overlap and then we gotta have this information if the pistol is overlapping the character mesh so if it is a weapon in the hand we don't want to simulate the physics because it's gonna generate some issues we're gonna get still get static mesh mesh of the pistol and then we're gonna set simulate physics to off so we're going to attach actor to component so as you can see here it says Target is actor and the actor is our gun so we're gonna get the pistol and the parent is our mesh so you can uh just drag out of the mesh here and then pin that to the parent and here location row we're gonna set is nap to Target all of them and now the socket name uh we need to know exactly where the pistol is going to be when we attach our app weapon to the uh to our character mesh so how are you gonna do that so let's go to the characters and type here on the search bar as key mannequin and we're gonna double click on this so this is the character this is the this is the let me so this is the skeleton of the the queen I think it's Queen difference the pronunciation so let's type here hand and so we need to know the exact location where we're going to attach our weapon to if you go here we can create an ad socket and then this is the name of our socket hand slash R socket so when you right click on this add preview asset and type here pistol um so this is going to be the location of our pistol of course it's not right we gotta fix that and okay it doesn't need to be perfect for the purpose of this tutorial but you can always uh rotate it and move it whatever place you want okay so we're gonna copy this uh socket here press f2 copy here Ctrl C and in the socket name Ctrl V okay so when we hit compile and press play when you press F we're not colliding with the Box Collision so nothing happens but when we overlap press f uh something is not working let me see what is it conclusion two pounds later [Music] uh sorry guys this is actually false so when we hit play and we'll run over the depboxylation Press f and then it uh attaches the gun so when I press F again nothing happens because we didn't set up the detached code yet but as you can see here when you press F and we are outside the box Collision and nothing happens and then we are here I can attach it so this is the code for attaching the weapon now we want to detach the weapon so how are we gonna do that we're gonna get the pistol attached we're gonna set the pistol attached to be checked on so that way we want to tell this variable that we already attached this gun um so now we're gonna collapse all of this let's collapse the nodes uh this you can do by preference I just want to have a more organized code so we're gonna name this attach let's double click on this and let's actually add the a print string here so we're going to say if we're going to say is 2 4 from weapon this is just for the purpose of the tutorial you don't need to do this so when the uh our character is away from the the pistol when you press F it's gonna tell it's gonna print on the screen that the the player is too far from the weapon so that way you know more what's going on at the code so okay so drag off this all right so now we're going to execute the the other code the detach code so we're gonna get a new branch and we're gonna get the information of the pistol attached so we're gonna know if the pistol is attached or not if the pistol is attached now we we're gonna want to detach this pistol so if this uh Boolean value is true then that means we already caught our weapon in our hands so now we want to detach it uh so now let's write detach from actor and let's drag our pistol here you want to detach our pistol all right uh let's run this code to see what happens uh okay so we have our pistol here we press f would pick up the gun we'll press F again and the gun disappears the reason why here is because we didn't tell the engine where location of the gun is going to be after we detach it from the actor so we're going to set actor location and rotation and the target is our pistol so we're going to set the location and rotation of the pistol when we detach uh from our player so are we going to get the the name of the socket we're going to get the relative locations and rotation of the socket so we're gonna copy the name of the socket here and we're gonna tell it look I want to have the socket location of the mesh uh and the socket name is gonna be hand or socket and we got our location now we need our rotation get socket rotation and we're gonna connect the rotation to the values and location that's pretty much everything you need to do on this here so when we press play uh we pick up our gun and then we press F again and there you go there is a this is where the PC is gonna be in so now that we detached uh the weapon so we're gonna set the pistol attached variable to false and we're going to simulate the physics because when we press play and detach it we're gonna have to simulate the physics again let's drag and drop our pistol first and get aesthetic mesh and drag off this and set Collision enabled and we're going to choose Collision enabled query in physics and after that we're going to set simulate physics finally to on so this is going to activate the Collision after we drop our weapon and this is going to simulate the physics after so let's run to our weapon press F and then press F again and there you go we dropped a weapon and it simulated physics as well this is well so uh one thing that I like to do here is organize the code better so you can right click and collapse into nodes and right here detach so this is attach and detach compile hit play and when we are away from the weapon we press f nothing happens and now we can pick up the gun there you go we can do something we can play with the weapon and uh learn how to throw it away I just gotta jump and then drop the gun all right that's the end of this tutorial I hope you enjoyed and I see you in the next video bye [Music]
Info
Channel: LocoDev
Views: 27,729
Rating: undefined out of 5
Keywords: physics, drop, physics handle, chaos physics, pickup, system, unreal engine 4 physics, physics constraint, physics simulation, weapon system, gameplay ability system, ragdoll physics, ue5 physics fields, destruction system, ue4 ragdoll physics, gun system, physical, unreal engine physics fields, unreal engine 5 physics fields, with, pickup objects, drop objects, how to create a game with unreal engine 5, and, unreal engine vr hands, how to pick up, pick up, item
Id: sPAPfQ1deuM
Channel Id: undefined
Length: 17min 17sec (1037 seconds)
Published: Tue Oct 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.