Collectibles in Unity 2D / 2021 (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video i'm going to show you how to create simple collectible items for your 2d game so starting off i just have a simple scene created here i have a player using the platformer player movement script that i created for my other tutorial videos and all i'm going to do is add a collectible coin and display it on the screen using some ui so first thing i want to do is i'm going to create an empty game object i'm going to change the position so it's zeroed out i'm going to call this coin i will go ahead and add a sprite and i created this simple yellow square sprite in a pixel art software so should be pretty easy to reproduce i'm just going through here and slicing it up so we only get the square for the sprite but you can do that any way you want so i'm just calling the sprite game object sprite and i will assign my test start for the coin so in our game view you can't really see the coin that's because the player is on top of it so i'm just gonna go ahead and move it over a little bit move it up i know we will also need a collider so i'm just going to go ahead and add a box collider set it to is trigger and i will scale the size of the collider to fit the coin 1.5 and 0.6 is about right so on my coin game object i'm going to go ahead and add a component and i'm just going to make a simple object script this might not be necessary for your game but i like to create it just so i can keep track of things easier all right we have it open in visual studio so i'm just going to go ahead and remove all that stuff and give it a public string i'm just going to call it id so if we go back to unity our script should update with this id field and i'm just going to call it coin the next thing we're going to want to add is a collect object script i'm going to go ahead and open that up in visual studio as well go ahead and remove everything and now i will create a private object called this object and inside of our wake function i'm going to say this object is equal to get component object so this will just grab our object script from the game object that this script is on the next thing i want to add is an on trigger enter 2d function and i will add an if statement saying if collision dot compare tag is the same as player collider and for this part to work in your unity game whatever collider you're using for your player you'll have to tag it as player collider so now i'm gonna add if the player touches this game object then i'm just gonna destroy the game object and then one more thing we need to add is on our coin object we can add a rigidbody2d and i'm just setting it to static so if we go ahead and test this by running our game when our player runs into the object it disappears now the next thing we want to do is add some ui saying how many coins we've collected to do this i'm going to right click go to ui and i'm going to import text mesh pro so now we have a canvas with a text object in there if we scroll out we can see this rectangle here is our canvas this is our text object i'm going to zero out our text object so it's at 0 0 in the center of the screen going to our game view you can see it in the center for canvas i always change it to scale with screen size i'm going to change the reference resolution to 1920x1080 i usually select expand admittedly i'm not too sure how well this works uh there might be a better solution out there for scaling your canvas but this is what i do so if we go here we can change this to coins collected ui i'll just set that equal to zero or set the text to zero right for right now i'm gonna go to our game view okay it's a little small so i'll just set that to 72. change wrapping to disabled i always like to center the height of the text i also like to just play around with these settings for the text this isn't super necessary but it's just something i like to do to make it look a little nicer all right so we can move this up to our top corner we can also right click on our canvas go to ui and add an image and we can select our coin i also have a material created i'll just set native size i'll just move and then i just move it so it lines up with our ui and i'm actually going to parent the text to the image so i'm just going to steal that name put it there then i'm also going to change this to score text just to keep things a little neater all right so now i'm going to add a new component called update ui i can double click it to open it up in visual studio i'm just going to remove all that and i'm going to add using tm pro so we can access the text mesh pro library i'm going to go ahead and add a private text mesh pro ugi ug ui i call this ui text all right and then i'm also going to add a serialized field private game object object refab and a line for a private string object id and in our awake function i'm going to set ui text equal to get component of type text mesh pro ugui set i am going to set object id equal to object prefab dot get component of type object dot id and what this is doing is for whatever prefab we put in there it's going to look for object script and since our uh and since our id is public it'll be able to grab that id from there next i'm going to add a private void late update and i'm going to set ui text dot text equal to player prefs dot get int of object id and then i'll just set that to string and the reason i put this in late update is because uh i want to make sure everything else that we're running runs before we update the ui the ui in this case is just a little less important than uh our player movement for example that way this doesn't get in the way of anything that this is trying to do i'm also using player preps here because it's an easy way to store data locally and that way i don't have to communicate between scripts uh if i were to store like the score here in the player script then i'd have to figure out a way to uh link it to this ui script in this way we can just get our data from a source that everyone has access to so i'm going to go ahead and save that and i'm going back to our collect object script and before we destroy the object i am going to set playerprefs.setint of this object dot id and the value we're setting it to is playerprefs.getint of this object dot id plus one this way every time we collect a coin before it's destroyed we get one added to the number of coins we've collected i'll go ahead and save this script and go back to unity and we need an object prefab for update ui script so in our prefabs folder down here in assets i'm going to go ahead and take the coin we have and just drag it in and now that coin is a prefab so going back to our ui text object i'm just going to take our prefab drag it into our object prefab slot for update ui script and we should be ready to go before i do that i'm just gonna go ahead and create an empty game object zero out the position i'll call it coins and i'm just gonna add a few more coins to the scene just to make it a little more interesting all right might not look the best in the world but i added some coins so let's go ahead and collect them as you can see our ui is going up but you may have noticed that it started out at 17. that is not quite right so i'm going to show you how we can fix that by going back into visual studio i'm going to select our player movements movement script just because generally how i like to do things as i like to load the player once at the start of the game and during scene transitions i have a don't destroy script so this doesn't get destroyed between that between the scenes loading so i'm just gonna add it here in its awake function which should only run once theoretically and what i want to add is just player prefs dot delete all and this will completely remove any player prefs that you have saved on your local machine thus resetting the count for your coins all right so going back to our scene if i run this again we should start out with zero coins and we do and when we collect them the score goes up all right but our coins look a little stale just sitting there so let's give them some animation to do this i'm going to open up our prefab for a coin object and i'm going to add an animator component down here i'm just going to go ahead right click go to add tab animation and i am just going to create a new animation called coin and something simple you can do is have the coin move up and down and spin that's a pretty traditional way of animating coins and rather than animating each frame of this coin spinning i'm just gonna do it the lazy way inside of unity and rotate it on the y-axis so to do this i'm just going to move the timeline selector to the about halfway mark for one second about half a second in and i'm just gonna hit the record button and go ahead and change this up a little bit actually so when you're animating something rather than animating the object that the animator game object is on you want to animate the children so i'm going to select the sprite and you know what before i do any animation with the spray i'm just going to make the collider a child object of the sprite so anything we do to the sprite will also have the collider following so now with animating the sprite i'm just going to move it up a little bit going over to the one second mark i'm going to make sure that the final position is the same as the starting position so when it loops it looks good and at the end i'm also going to enter 360. actually let's make it negative 360. so it rotates the other way all right so that's how it rotates for the coin and to make this a little more seamless i'm gonna right click i'm gonna select the first keyframes right click and i'm gonna set them to auto same thing with the last key frames so now when we play it we got a spinning coin so when we go back to our scene and run our game our coins should have a little more life to them and that is essentially all you need to know to add collectibles to your game thank you very much for watching if you enjoyed this video you might enjoy some of my other tutorials link down in the description or there will be a card or some sort of end screen showing you where you can go see some of my other tutorial videos if you like this video go ahead and leave a like down below leave a comment telling me what i should do next and thank you very much for watching bye
Info
Channel: MichaelsGameLab
Views: 1,911
Rating: undefined out of 5
Keywords: unity, unity2d, unity3d, unity collectible, unity coins, unity tutorial, tutorial, programming, unity c#, unity animation, unity animator, 2D, michaelsgamelab, colliders, rigidbody, game dev, gamedev, game developement, devlog, learn unity, beginner unity tutorial, beginner tutorial
Id: vflF2PBPfv4
Channel Id: undefined
Length: 17min 52sec (1072 seconds)
Published: Fri Apr 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.