Item Drops (2D Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right so in this tutorial we're going to take a look at how to make item drops so that when you destroy an enemy an item appears pops up in the air and then lands down so you can go and collect it let's get started all right so in order to get this work you're just going to need a couple of things first of all obviously you're going to need a player character that can move around and do things you're going to need some sort of a condition so that the item can drop in my case it is my enemy who will be destroyed and when he's destroyed the item drops and that's about it to get started so first thing we're going to need to add here is we're going to need the actual item that's going to be dropped i'm just going to head over into my hierarchy here right click and i'm going to create a 2d object sprite and i'm just going to make a square for now and for the moment this will be my item i'm not i don't have a graphic prepared or anything like that i'm going to make it a little smaller because that seems a little out of control and i'm just going to go into my sprite renderer here and i'm just going to move it forward in the layers a bit so that it appears above some of my other graphics you may or may not need to do that but i want mine to appear in front we're going to need a couple of characteristics for this thing first of all because we're going to be adding a force that makes it pop up into the air it's going to need to have a rigid body 2d so we're going to add that component and we don't need to make any changes here we do want it to have gravity so it falls and that sort of thing so we can just leave that i'm also going to add in a box collider the reason we're putting the box collider on here is simply because when the item spawns it's going to bounce up in the air and when it comes back down we want it to actually collide and land on top of things not to fall through the floor down into the center of the earth all right at this point we're ready to add our item drop script so i'm going to head down here into my project tab and go into my scripts folder where i'm going to create c-sharp script i'm going to call mine item drop all right so there's just a couple of things that we're going to need to do here first of all um what we want this script to do is this is going to be the script that's in charge of making our item bounce up into the air when it is first instantiated into the game so this script won't make it appear but it will give it the behavior it has once it does appear so we're gonna head right underneath the class here and there's just a couple of things we need to do first of all we're going to need a reference to the rigid body that is on the item itself and we'll call this one item rb for item rigid body i'm making this one private because as you'll see in a minute you won't be able to set this one from inside the unity editor and we'll have to do it through code instead so down here in our start menu as soon as this item gets spawned into the game we want it to find its own rigid body so we're going to say that item rb is equal to and all we want to do right now is it's going to look on itself for the component called rigidbody2d once it finds it it will now know that anytime we say item rb we're talking about its own rigidbody all right next thing we're going to do is we're going to create a public float remember a float is a decimal number and we'll call this one drop force and essentially this is just going to be how much force is applied that makes it go up in the air before the item when the item drops i'm going to set mine to 5 to start but because this is public we can adjust it in unity later on until we get it just right next thing we're going to do is down here also in the start menu is we want it so that the second this item is instantiated so as soon as it starts it bounces up in the air so right here we're going to get our item rb and what we want to do is we want to add a force to it so you type in add force and then in our brackets what unity wants us to do is define a couple of things first thing we're going to define is the direction and then second will be the type of force so in this case it's going to be a vector 2 dot up and vector 2. up is just what it sounds like it has a y value of 1 you can actually see the shorthand here where it says that zero on the x so it's not moving left to right and one on the y meaning it's going up now we need to tell it how much how much force so we're going to multiply that by our drop force we'll put a comma and now the next thing that unity wants to know is what type of force it is we've actually got some options here we could do a force or we could do an impulse we're going to select impulse a force would just apply the force um gradually kind of like a normal force being pushed on something where it kind of accelerates over time whereas impulse hits it with the force right away all at once and then stops which is the effect we want for something bouncing up into the air and we aren't going to be doing anything in our update method in fact if you wanted to you could take that out of there altogether um you don't have to all right this one's up and running now let's head back to unity all right so back in unity what i want to do at this point is just add the script we just wrote so i'm going to head over to the inspector click add component and start typing in item drop there it is excellent and you'll notice here we have our drop for so we can change that later on if we want this one i'm just going to hit play you'll notice as soon as things start it bounces up in the air looks pretty good to me at this point we're gonna make our item drop into a prefab now if you haven't worked with prefabs before they're simply pre-made game objects that can be spawned into the game later on during gameplay especially useful for things like projectiles or item drops now this is actually very easy to do first of all just in your project tab here under your assets i'd recommend creating a prefab folder to hold all of the prefabs that you make and then all you do is you just take your square drag it down into that folder and you'll notice when it goes down there that it turns blue that means that it is a prefab and that's all you have to do you've now created the prefab and we can spawn it into the game i can now take this one out of my game so all that's left to do now is to actually make it so that the item drops now depending on how your game is set up this could look a little bit different you'll have to decide what your drop conditions are in my case it's when this mech character here gets destroyed and so i'm going to go into my assets and find the script that handles his health and that's where we're going to put this line of code in all right now there's not a whole lot of code that we have to do here there's really three things first we're going to make a quick reference to the game object that we want to spawn into our game in this case our items that are going to be dropped so we'll call them item drops now one thing we're going to do that's a little different than normal is at this point it only allows us to have one item being dropped but it might be that you have five different items you want to be dropped from an enemy so what we're going to do is we're going to make this an array we're going to put square brackets here after game object and what that means now is that in unity now when i click on my mac and i go into his health script you'll now notice that there's this item drops and it's got an empty list what i can do from here is i can go and grab my prefab in this case it looks like i forgot to rename it it's called square and i can drag it over here and just put it on item drop and it will now show up in there and so now the game knows if i wanted to drop multiple ones i could keep going and it would add to the list as many items as i want but i now have my item actually my enemy knowing which item he's going to drop now at this point we're going to head all the way to the bottom of our script just inside that last bracket and we're going to create the function that's actually going to cause this item to be dropped so we'll make it private it's not being called from another script so we can keep it private and we're just going to call this one item drop don't forget your curly brackets and then in here is we're going to actually write the code and essentially what we want to do is make a loop we want it to look at our list of items and for every single item that's in the list we want it to drop it and to make it pop up into the air and that sort of thing so the way we're going to do that is by using a for loop so we type 4 and then in brackets we're going to first of all create a variable it's just an integer called we'll call it i um that's sort of the standard and it's going to start at 0. and essentially that means it's going to start at item 0 on the list and it's going to do make it instantiate then it will check to see if there's an item one and if there is it will spawn that in and it'll keep going until it runs out of items in the list so it'll start add zero and essentially as long as i our integer is less than and it's going to look now at our item drops dot length meaning the length of the list so as long as this number is still lower than the number of items in our list it's going to keep running this loop we'll do a semicolon and then we just put i plus plus that just means that each time it runs the loop it adds one more onto our integer so the first time it will do item zero then it'll check to see if there's an item one and it'll run the loop and it'll keep going for as long as i is less than the number of items there are we'll do a curly bracket here and now we just need to tell it what is actually going to happen and in this case we want to instantiate meaning spawn in our item now anytime we use instantiate unity is going to want to know a couple of things it's going to first of all want to know what the item is that's being spawned in so in this case it's going to be item drops and we put a square bracket with an i in here so the first time it will spawn in item 0 and then if there's more items it'll spawn in item 1 and 2 and on and on at this point we can put a comma now it just wants to know well where do we want the item to appear and in this case we want it to appear where the enemy is so i'm going to type in transform.position so to look at the enemy because this is on my enemy it'll look at his transform component and check his position and that's where it will spawn in the item i'll do another comma here and it just wants to know if it's going to rotate the item at all and we just want our item to stay with its current rotation so we're going to type in quaternion which is like a vector for rotations it just means it's good it's about to get a variable that tells it the rotation and we'll put dot identity meaning it'll keep its own current rotation you can semicolon that up and the next thing i'm going to add one little tweak to this it's up to you if you want to do it but i actually don't want my item to spawn exactly on top of my enemy i want it to spawn just a tiny bit above him so here in transform position i'm just going to add plus we're going to add a coordinate for how much higher we want it so we'll take the new vector 2 and the vector 2 in this case it's going to be 0 on the x meaning it'll be it won't be to the left or the right and i want it to just be 1 unit above oh and sorry because instantiate works with vector threes i also need to add a z value which will be zero one last piece of coding actually is we just need to tell the game when to make this happen in my case i want this item drop function to run when the enemy's health gets to zero or lower so i'm just going to find the part in my script for me that happens here in my update method this is where it looks to see when my max health gets to be zero or less and this runs my animation and kills him and all that stuff you don't need to worry about that all we want to do here is just type in the name of our function which is called item drop put your brackets and so now when my mex health gets to zero it'll run all my normal things but it will also call this item drop function down here which will run through my items and make sure that they pop into the game there's nothing special you have to do from here at this point i should now be able to destroy my enemy and the item pops up in the air excellent just the way we want it alright thanks so much for watching this tutorial if you enjoyed it please be sure to click like or subscribe to the channel until next time this is matt with nightrun studio cheers
Info
Channel: Night Run Studio
Views: 4,904
Rating: undefined out of 5
Keywords: Unity, 2D, sidescroller, platformer, tutorial, item drop, spawn items
Id: Ud0Qqo5ytkI
Channel Id: undefined
Length: 11min 35sec (695 seconds)
Published: Wed Aug 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.