How To Pick Up an Item - Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so you're making a game and you want to make like a pickup system where you pick up something it can either be a flashlight a torch a spear a sword you name it I'm going to show you exactly how we can do that it's really simple and really simple to understand as well so in this scene I'm going to be working with a flashlight that I picked up from the unity asset store is completely for free I will leave a link in the description if you guys want to use it it's really cool as like flashlights and audio Source attached to it so it's really cool um so let's dive straight into this so the first thing I have here my first person controller which I also picked up from the unity asset or it's also completely for free I'll also leave a link in the description first thing we want to do is grab our object that we want to put on our player and I'm going to duplicate this so Ctrl D there we go click and drag and what I'm gonna do is I'm actually going to child it under our first person controller so childhood click and drag drop it in I'm going to set these values zero zero zeros just so that is in the middle I'm going to dock my game view to the right and really this is how you want your object to be shown to the player so I'm going to put it up a little bit to the right a little bit forward give it a rotation like that I guess maybe a bit up there we go this is how I want my object to be shown once the player picked up the item so come back this is how it's gonna look oh in our object that we're gonna be picking up or actually sorry go back in our first version controller and disable the flashlight back in our pickup object I'm actually already going to put a book School lighter on this make sure your set is on trigger and we're going to give this I don't know seven seven like that then we're going to create our script now before I even create my script we're going to go on to our first special controller and make sure that you have selected under the tags layer very important okay so let's create our script C sharp script I'm just going to call it pickup pick up uh flash light there we go we're going to open it up all right so we don't need our update uh let's get this okay so we're gonna say public game object uh we're gonna say Flash lights on player so in the beginning of our scene just in case we're going to say is active false then we're going to create a on trigger stay not the answer to stay and I'm going to say if other Dot Game object Dot tag is is player next thing um so we're going to say we're going to use the input system so if input dot get key cheat code dots e now you could use f whatever that you like so once we click on our you know how we pick up our item we're gonna disable uh the flashlight that is in the scene so we're gonna we're just going to say this Dot Game object dot set active false like that and we want to or actually I'm just gonna copy this line paste it under and set it to true and that is actually it so we're going to go back in our scene let the script reload I'm going to click on our flashlight click and drag in our script and our gamer bucket that we're going to be putting in here is the game object that is the flashlight in our first person controller so click and drag it in like that let's press on play this should work and if I press on E there we go the game object is disabled and we have our flashlight enabled on our player okay if you're already satisfied with this you can stop but I'm gonna go a little bit further with this so in our canvas if you haven't already make one so create UI canvas or is it canvas this one in here we're going to create a new UI element which is a text I'm going to say pick in here or actually I'm going to go into 2D mode press on F zoom out a little bit I'm going to say press e to pick up something like that now I want everything to be in one line so I'm just going to click and drag it and just align it a little bit in the middle go into game view let's see how it looks looks pretty good literally jet is flying over my house right now but I'm gonna Center it I mean you can play with these values it's just like an outline I mean we'll create an outline why not let's say one oh zero point uh two now why not okay zero zero point one there we go okay so go back in our go to our player in our flashlight script open it back up we're going to create another public game now you can use uh like your y elements but honestly this is a little bit more simple and it's way easier to understand so we're just gonna say pick up uh text so yet again same thing selective false and what we're going to do I'm just gonna copy this line paste it right before the second if I'm going to say set it to true so whenever that we enter the trigger when I pick our you know we want to enable that text this uh close up the script go back in our scene okay and we want to click and drag in our text there we go now if we press play now we go in the trigger it's going to say press e to pick up we press e but now we have an issue right the text is not disappearing well very very simple solution for this is just paste this under here set it to false and we're going to say void on trigger exit paste the exact same code here and make it false because if we let me show you what happens if we don't Scripts so if you don't put that on trigger exit we get this we go in we go out and it's it's there forever you know what I mean so if you put it back on Ctrl U H save all right and then press on E there we go that works and it should also work if I only enter and exit the light up the outs there we go look at that perfect okay guys so that's actually gonna be the end of the video I hope you enjoyed make sure to subscribe and like I guess more so every time when I try to make a video my family always interrupts me so anyways guys hope you enjoyed make sure to subscribe and like because more tutorials are on the way
Info
Channel: Gunzz
Views: 30,331
Rating: undefined out of 5
Keywords:
Id: zEfahR66Pa8
Channel Id: undefined
Length: 8min 58sec (538 seconds)
Published: Mon Oct 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.