Collect Items in Unity - Game Dev Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everyone John stay scale here welcome back to yet another game dev tutorial so we're gonna have a bit of fun today I'm pretty excited we're gonna build a simple item collection system which allows the player to walk into and make contact with different item types money potion and maybe like a powerup and store those different item types in an array that can be then processed in different ways alright let's get into it [Music] so we've got a sample scene here and in my library here I've just got a few different item images that I've created so these are just images and if we run this now we'll see when the player walks into it nothing happens so our objective here is to make contact with those items make them disappear and then store them in some kind of a way that we can use and process them into meaningful ways there's a few different first steps we can use here but I think the best way to go about this is to attach box colliders to each of all not necessarily box colliders but colliders so we'll start with a coin then we jump over here add component and we'll say we'll say circle Collider 2d as we're in 2d and importantly we're going to check this trigger box if I have an unchecked and the player walks over to this thing look at that that's outrageous yes so the player yeah he's making hard contact with the coin so we want the coin to be a trigger in this case so we can pass through it like so we'll just go and do this with the rest of these other items and you just want to kind of fine tune your colliders to match whatever the type of shape you're working with this one's a bit of circle so a ball like that doesn't have to be perfect for now but that'll do if you want to use the exert so I'll put a link down below you can pick them up and use them for your own experimentation I don't want to see them in any games actually go ahead do whatever you like so finished off with this fun add another circle type okay so now we have three items of different types and they all have colliders of slightly different proportions the next step is to create a new tag from the tag list and assign them to these items if you're building a small game you can create a new tag for different items so you're gonna have a one tag called coin one tag called potion one tag called powerup and that's fine because we've only got three different items but if your game grew to a very large size and you had you know 20 different items it's not really feasible to at that point have 20 different item types rather you want to have one tag type like collectable or item collect and then process the type in a script so that won't make too much sense right now but I'm gonna show you what that means and are we gonna elaborate on it so in this tag list so you can get to the tag list by clicking any one of these different items selecting tag up here and just go to add tag and you'll have a bunch of default tags that unity is created so good at tag and here we'll add plus and here I'm gonna say collectable save and we just click back on that item and we just want to make sure that that collectable tag is assigned to the coin we'll do the same for the potion collectable and we'll do the same for this star right so now the next step after that would be to well actually let's jump over to the player and bright in the collision code to make contact with this new collectible item type so in this case I might just write at the collision code on the main player script so we'll double click and open the player script so importantly we're not going to do on collision enter because we made our collision items trigger we tap that is trigger box so I'm gonna do it on trigger enter 2d and if you just find that from the order complete Visual Studio will do a nice job processing that for you so what this means is any time we make contact with a trigger Collider like those items this function is going to fire and it's going to give us some information through this this tells us what we've collided with let's do a console log let's go print gliding just to make the point that to show this is actually working now we just run that and if I walk over to the coin boom you see that down here if I opened it up colliding and if I walk into the next one right so we've got that issue here where we didn't make them all triggers so it's gonna go through make sure that and notice that that's actually I'm glad that happened because notice that because that wasn't a trigger that code that code he did not fire he'd only fired on the trigger if I had a on collision enter to D now then yes it would have worked so that's an important distinction because your item types your item collectables don't necessarily have to be triggers if for whatever reason you need them to be hard items then you can use this function and run both of these simultaneously for now we're just going to be working with the triggers because this is in my opinion the more common kind of item type collision you're gonna have so I'm just gonna make these all triggers it's gonna run that again and we should now get three different separate locks one two three so if it does open this again you can see I've got one two three right so now we know we've collided with three items that's cool but we've got three different item types so we need a way to differentiate to the player what he's collected because the player wants to know the difference between a coin and a potion because if it's all being processed as the same thing then what's the point if you've only got one item type in your game sure and some very simple games might only have one item type like the pac-man has coins but then again pac-man also has those big coins that let you then chase the guy so you need a way to differentiate between items so one important note is this function here will track collisions with all triggers so we want to limit our collision check here to only those collectibles with that collectable tag so what we'll do is we'll say if collision so we're referencing this one here dot compare tag so we're checking the tag of the object reclining with and we'll say collectable which is that tag we created so if the if the tag of the item we collected hitting is called collectable then prints we have collected an item and what we can do here also we can say collision dot game object dot just ROI basic destroy collision dot game object so we're just going to now remove the game object from the screen when we make a contact and I just want to show you something now I'm just going to duplicate this last star item okay and I'm going to take that collectable tag away untag just to show you the difference here because in our check this out boom boom boom three you have collected an item three times and now watch this the last one is no longer working because we have limited the collection - only those items with a collectible very good so now we have a way to collect only collectable items okay but now we still need to differentiate between which type it is right we know it's a collectible but now we want to take the collectible and store it we might create an array now a list specifically that we can store the items so you stick and ignore all this stuff I have here this is to do with the players movement and the input up here we're just going to say and we'll say list and we're going to make this off type it string am i doing I need to give this finger name items excuse me I don't know what the hell I was trying to write this event but it was insane so we are yeah the second video of made today so I'm running out of steam so anyway so yeah a list is a type of I guess the type of array you could say just but you can add to it as you go where with an array in unity you need to kind of declare how big the array is up front and then what we just want to do is in the start function we just once the items equals new list and we just need to pass in a string and while we passing in string it means it means that this list site can only take string types when creating a list you need to define what type of data type you're going to put in is it a game object is it a string is it we could make this game objects we could say game objects and we could store um the game objects in here but it's kind of unnecessary because it's a lot more lightweight to store the string because if we stick the game objects into the into the list then that list is holding a whole bunch of game objects and we don't need to Reno all those collectable game objects we just want to know what they were like the reference name or something like that so that's why we're doing it as a string and what we need to do now we want to create a collectible script so what we gonna do we can click any of these collectibles here we just got add component now let's say new scripts say click the bold script and we'll click create an ad so that's now created a collectable script for our coin and what we'll want to do we just want to go through the other items we've created and also just add that script you can kind of select them all in do it or just a one by one whatever so we'll open that script up now so we're gonna say public item type sorry public string item type that's the second time I've overridden it you know in that back-to-front way and I can get rid of this don't need that all we need is the item type exposed here so this will now expose the item type of field so now look at this we can say up here we can say coin you can say potion and we can say I'll just say power whatever that is and then we will go back to our player scripts so here now check this out now we can get that information the item type will say string item type equals collision dot game object dot get component collectible scripts remember we create that collectible script just running out of space yeah we'll do open and close brackets dot and now check this out we now get access to that item time very very nice we'll just put this print down here so not rather than just saying we have collected an item we have collected a and I can now pass in that item type here so now we have the reference to the item type and now we can actually stick that so the items lit that item is a list we created that we can say items dot add and we can type pass in that item type we are storing them uniquely into an array and that will that list rather and that list will grow and grow and grow depending on how many items we have and then we can do different things with that we can create a inventory management screen and populate that screen with all the different items and that's something that I won't do in this particular video because that will just make this video over an hour long so I will make a video in a near future showing you how to display this list in a meaningful way maybe use it add it to the hard maybe like a coin counter or something like that and some of you may already know how to do that so now that we've done that we can also what I'm gonna do I'm gonna print out the length venturi links I'll say items dot count so the count gives us the length of the inventory and you know what I call this items but I kind of feel like I should have called that inventory just kind of Sam sounds more gamey and sounds cooler so excuse me for this little change but let's just change this to items between venturi so all you got to do is just go back up and I probably should have done that to begin with but items is fine whatever all right so let's run this now and see what happens but console should now not only log out the name of the item we're collecting but also then the length of our inventory okay so you have collected a coin and your inventory length is one an hour look at this you have collected a potion your inventory length is two you have collected a power your inventory length is three so that works perfectly so now we have a mini inventory system and a way to populate it with different item types so I hope you found this video useful guys if you have make sure you give it a big thumbs up down below for me I'd really appreciate that thanks to my monthly patreon for supporting me and this channel I really couldn't do this without you you motivate me and help me to continue creating quality content for you guys so if any of you guys wanted support I'll put a patreon link link down below support has also get access to all the different tutorial files and source code and other useful game making scripts including the full complete project file for this particular video alright guys see you in the next video and as always all the best on your game dev adventures [Music]
Info
Channel: Lost Relic Games
Views: 15,432
Rating: undefined out of 5
Keywords: collect items in unity, collect items unity, collect items unity 2d, put items list unity, items into list unity, items into array unity, csharp for beginners, unity inventory system, unity collide with items, item collision unity, pick up items unity, unity 2d tutorial for beginners 2020
Id: bvlPtAErGp0
Channel Id: undefined
Length: 17min 10sec (1030 seconds)
Published: Sat Mar 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.