How To Pick Up And Drop Items | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to a new video today we are going to or I'm going to show you guys how you can actually pick up an item and drop it as well uh ever since my last video on how to pick up an item I got loads of uh questions saying oh can you also show us how we can drop that uh object or camera bag whatever right so let's get straight into this so the first thing that we need is I did reset the whole scene so yeah let's start over I'm not going to be using a flashlight in this video I'm going to be using a gun okay so I'm going to be using this little butt bit gun right here which I got from the asset store uh this has you know the basic stuff it isn't uh like it doesn't have any children or whatever that's exactly what we need now the first few things that we're gonna need are all the components obviously so first of all I'm actually gonna do 90 just so that looks like it's laying flat there we go then we gotta add a rigid body leave it as is that's good enough then we're going to need a mesh Collider so that the gun doesn't like fall through the floor let me pick it up next thing we need is a box collider as the trigger I'm also going to be I don't know if it's five five five uh yeah something like that why not now let's create this script because that's where the font stuff begins okay so let's create a script see short skip I'm gonna call this equip oh it happens equip weapon or whatever call this whatever you like so what I'm actually gonna do guys is oh actually let me go back sorry I forgot something because we're going to when we pick it up childhood underneath our camera our first person camera so in here because um we need a parent and I guess you could use a camera but honestly or not no wait hold on because if you use the camera then the gun is going to like spawn or you know get put exactly where the camera is so we won't be able to see it so we're going to create an empty object in or you know as a child of the camera I'm going to call this weapon hair and something like that okay that's good now this is going to be where your gun is going to beat so I'm just going to put it Forward down and a bit to the right I mean we can adjust this uh as you like but I guess that this is pretty good uh we'll see how it looks when we actually do pick up the weapon okay so going back to the script uh where's the script there we go let's get rid of that uh we yeah we do need to start because yeah okay so the first thing we're gonna need two variables so the first thing is going to be for the gun itself I guess you could you know say this dot gamerpiac but honestly this is there we go it's got the gun now we're going to need a public transform and this is going to be the weapon parents that we just created so in our start methods because we don't want a weapon uh to be you know to have gravity right on the start so we're gonna say gun dot get components the rigid body and we're going to say is kinematic true just for safety reasons that we don't need it okay so um in our update I'm just going to say if inputs. get gets key key code this is going to be for our drop so I'm just going to say f we're going to pick up our weapon with E and drop it with f yeah and then we're gonna create a function called drop here I'm going to say void drop we need to set the gun it's transform uh or we need to give it like a new um effective tree so what I'm going to do is we're going to say gun so it's friends form dot ruler angles is a new factor three gun dot transform dot position dot xor yeah comma and copy this line and we're going to say set comma paste it again and it's going to be the Y okay so obviously when we drop weapon we're going to give it like a new uh you know position or you know earlier angles whatever then we're going to say gun dots get component actually forgot to do something but I'm gonna do it just in a minute don't worry okay component the rigid body builds um is kinematic false because we want the weapon to fall to the floor yeah and and also we need to say gun so it's jet component mesh renderer or mesh collider story oh my God dot enabled is true and we need to say gun parent or wrap and parrot whatever detached children because we want you know to detach the child makes sense okay so that is for our drop now we're gonna do the equip to avoid equip okay so when our equipment weapon we're gonna say dot get components the rigid body so this is when we picked up the gun I'm gonna say it's kind of met is kinematic not true sorry true I would say gun built transform built position is going to be the weapon parent Dot transform.position copy this whole line paste it underneath and this is going to be the rotation just like so um and also we just for safety reasons we're going to say go wait hold on I'm just going to copy this line I'm gonna paste it here and set it to false now to be able to set it as a child this isn't just going to work right this is not going to be um oh hold on we called our script equip weapon so we cannot pull our function the same name that's why it was red oh yeah and then we say gun Dot threats formed set parent is going to be our weapon parent there we go you just say gun get a transform I set a note payment which is our weapon parent okay that is good and now obviously to on trigger stay to pick up the weapon we're just gonna say if other thoughts game up yeah it's about attack is this play your uh and the if kids cheat codes e is pretty much the same as the previous video and then we just want to call this a function sort of equip and that should be good yeah that should be good let's close it off did it save hello I guess it did so in our gun so this one we're going to put that script so it could open very cool our gun is going to be our gun and a trans or the weapon parent is going to be the this one click and drag it in and now if we press on play we should be able to pick up and drop the weapon he picked up the weapon I mean you could change the weapon Parable if I go to the zoom out a bit there it is here I mean it looks pretty cool and then when we press on F it should drop press on E we pick it back up F we drop it so yeah that is going to be it if you guys want I don't know any more tutorials or an expansion of this be sure to let me know in the comment section below but uh yeah I'll see you guys in the oh yeah hold on just a second if you guys want the same layout as my as mine you can go here into layout and then click on what was it was it tall yeah tall you will get this and just drag and drop the game view underneath and what we can also do is win the window and consoles here we go and then we put it here and yeah that's actually going to be going to be it guys I hope you enjoyed enjoyed oh my God I cannot even speak sorry guys but I'm a bit sick so my throat hurts so but yeah hope you guys enjoyed the video make sure to subscribe and like and I'll see you guys in the next video bye bye
Info
Channel: Gunzz
Views: 5,395
Rating: undefined out of 5
Keywords:
Id: V9VbwpsvmZo
Channel Id: undefined
Length: 11min 17sec (677 seconds)
Published: Sat Feb 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.