Let's create our coin.
So I have the coin sprite in my UI folder, as you can see in project tab and then assets folder.
So I'm going to select my this coin.
So let's select this coin sprite.
Let's go to Inspector.
Click on this open Sprite editor.
So we can see we have more than one.
So we're going to slice individually.
So we'll choose sprite mode to multiple filter mode to point no filter.
And let's click on this apply.
And let's slice it individually.
Basically what we're going to do we're going to slice grid by cell count.
So let's click on this slice.
We're going to choose automatic to create by cell count.
And let's count how many columns and rows we have.
So 369 is nine columns.
So I'm going to put nine columns not nine column exactly nine 1011.
So we're going to put 11 columns and one row.
After doing that let's click on this slice.
And then let's click on this apply button.
And now it's fine.
So I'm going to select my game view once again here.
And let's click on this arrow by now and I'm going to choose the first one.
So let's select that.
And we're going to drag it over to this in view or in this hierarchy.
So as soon as we drag it we can see.
But the problem is that is it is too much small.
So we need to make it bigger in size.
So I'm going to select our sprite sheet one more time.
And let's make this sprite uh per unit something around 16.
And let's click on this apply.
And now we are able to see if you just want to make it a little bit bigger.
Then you can just make this a pixel per unit even smaller number.
So I'm going to choose something like uh 12 to make it a little bit bigger.
Let's click on this apply.
Now we can see it seems fine.
So we're going to do the same thing as we created this heart uh heart game object.
Basically we're going to have two animation.
Uh, first will be this coin spinning which will spin around itself.
And the second, uh, animation just going to be the collected one.
Basically it will go up and over time it's going to fade out.
Basically, we're going to do the same thing as we have done in this hard game object.
So let's create an empty parent game object first because we're going to have to animation clip and
the collected one.
So that's why.
So let's go to hierarchy.
Let's right click or hit this plus icon.
Let's create an empty game object.
And I'm going to call this game object to something like coin.
And and I'm going to select this uh tool move tool from this in view.
And I'm going to choose an gizmos for that something this or this we can choose I'm going to choose
this one.
And I'm going to drag it top up in my hierarchy so I can see all the time.
And now I'm going to select my this coin sprite.
So let's select this coin sprite.
And firstly I'm going to name it to something like coin sprite a coin sprite.
And let's select this coin sprite in the hierarchy and drag it over to this coin game object in hierarchy.
And we're going to go over to transform of this coin sprite.
Let's right click there and reset.
So it's going to reset the position as the position of our coin game object or the parent one as you
can see.
So let's add some component.
So first thing we're going to add a rigid body 2D for a gravity.
And we're going to set the gravity equal to zero.
So it's not going to fall down due to our gravity.
So let's select the coin parent one.
So the select the parent one not the child.
Select the parent.
Go over to uh go over to Inspector.
Click on Add Component.
And we're going to add a rigid body 2D component.
And let's set the gravity body type to kinematic.
So it's not going to fall down due to gravity.
And we're going to choose collision detection to continuous and interpolate to interpolate to just making
out smooth.
Or you can just leave it to none as well.
This interpolate section.
But I'm going to choose interpolate.
And let's add one more component which will be the collider 2D for collision detection.
So let's click on Add Component and I'm gonna add a box Collider 2D to just fit it perfectly.
And let's click on this Edit Collider to edit the collider as the size of our coin sprite.
And we can see this is fine by now.
And make sure you just go to trigger and check this one underneath your box Collider 2D component.
And now we are ready to create our animation.
But before we create, let's assign this coin game object as a coin tag.
So let's go to Inspector.
Let's click on this untag add tag.
And then we're going to click on this plus icon in order to add a tag.
And we're going to create a coin tag something like that.
And save it.
And make sure you just go over to that GameObject once again and assign it the tag which you recently.
So name it.
So I assign it the coin game object as a coin tag.
As you can see in Inspector, and we are ready to create our animation.
To create the animation we're going to select the tile game object, not the parent.
So we're going to select our child.
And let's go to project tab sprites and then UI folder.
Basically move over to that folder where you have the sprites.
So I'm going to select all of these.
So let's click on this arrow.
And I'm going to select all of this coin sprite by holding down shift shift.
Basically this coin individual sprites.
And I'm going to drag it over to this coin sprite GameObject in the hierarchy in order to make the animation
right away.
And it's making our animation right away.
Instead of instead of moving over to this animation tab and then click on that create button in order
to create the animation.
And this is pretty much first way.
So you can just select all of the individual sprite, and then you can just drag it over to top of that
game object in the hierarchy in order to make the animation even faster way.
So I'm going to move over to my assets folder.
In assets folder I have created this animation folder, so I'm going to open that up as well as I'm
going to open up this stop animation.
And I'm going to create a separate subfolder for my coin animation.
So let's right click the new folder.
And I'm going to name this folder to Coin Underscore Animation.
And let's open up this subfolder as well by double clicking on it.
And we have to name the animation.
So this one is just going to be the spinning one coin coin spinning.
So I'm going to call it coin underscore spinning spinning.
And that's fine.
And let's click on the save button.
And it's it's going to create the animation right away.
So now if we select our coin sprite in the hierarchy move over to animation tab and then click on this
plus icon.
We can see indeed the coin just spinning around itself.
And this seems pretty much cool.
And we're going to have another animation which will be the collected one.
Basically this coin will go up and over time it's gonna fade out.
So let's create that animation.
So we're going to select our coin sprite in the hierarchy.
Let's go to animation tab.
Click over here and we're going to click on this Create New clip.
And this is going to be the coin underscore uh the collected one.
So I'm going to call it collected or collide one.
So let's uh I'm going to call it collected instead collected.
And let's click on the save button.
And we need to make the animation.
So let's click on this red button in order to record the animation.
And I'm going to move over to 60 frames or one second.
And I'm going to select my coin Sprite GameObject in the hierarchy.
And let's move it up direction.
And then over time we're gonna, uh, we're gonna make it fade out.
So let's select the coin sprite, let's go to sprite Renderer component and a color.
Let's click there.
And in the bottom A which is 255.
So I'm going to make it all the way to the zero.
So once we make it zero we can see the coin just gets invisible.
So we can just click on this play button in order to uh see the animation.
So it's just going up and over time is just fading out.
As you can see.
And once you're done, you can just click on this red button to capture the animation.
Or you can just leave it like that.
It's automatically gonna capture the animation.
So now if we select our coin sprite GameObject in the hierarchy, move over to animator tab.
We can see we have animation where we have coin spinning as well as the coin collected.
So we also need to make transition between these animation state.
So let's right click on this coin animation spinning.
And we're going to make transition to coin collect it.
And let's select this transition.
Go over to Inspector and we're going to untick.
This has exit time as well as we're going to set the transition duration to zero.
And we need to choose a condition in which condition we want to play the animation clip.
So let's go to animator tab and let's select this parameter.
Let's click on this plus icon.
And we're going to choose a trigger in the bottom.
And I'm going to call this trigger to something like collide as I have done in my hard game object.
So basically we're just approaching the same way.
And now I'm going to choose this color.
So let's click on this plus icon in the condition Inspector.
And now if we just click on this play button and manually just gonna manually try to trigger this collide
parameter.
You will see the coin collected animation.
Just gonna play right away of this coin sprite as soon as we just trigger it manually.
So if I just trigger, as you can see, as soon as we trigger it's just going up and over time it's
just fading out.
But the problem is that this animation is just looping.
Basically it's just playing over again and again.
So we want to stop that.
So let's get out of play mode and we're going to move over to my project tab.
Then assets then animation basically where we created or just place those animation clips.
So we're going to move over to that folder.
So I have this coin underscore animation.
And we can see we have both of this animation clip.
We have coin spinning as well as the coin collected.
So I'm going to choose my coin collected and I'm going to uncheck this loop time in the in the inspector.
So the animation clip will not loop.
So now it's fine.
And we need to make this coin a sprite coin GameObject in our hierarchy a prefab.
So we're going to have the copy.
So let's select the coin GameObject in the hierarchy.
And let's drag it over to this prefabs folder in order to make it prefab.
Now we just made it prefab as you can see, and we are ready to collect that one.
Uh, so we just created and so we also need to uh, collect this coin, uh GameObject.
So let's do that.
So I'm going to select my player game object in the hierarchy.
And let's open up the player script by double clicking on it.
And what we're going to do we're going to use this same function one more time which is ontriggerenter.
So this function get called when we trigger with something.
And if we move to unity then our coin and then choose this coin sprite.
Uh coin sprite coin game object, not the child one.
Uh, and move over to Box Collider 2D.
We can see we just check this box.
So that's why we're going to use a ontriggerenter 2D function in order to, uh, check the trigger.
And I'm going to move this coin sprite game object a little bit down.
So this player game object easily can collect that one.
So we assign it this coin game object as a coin tag.
We can see in the inspector as well.
So let's go to script.
So this function get called when we trigger with something.
And we can extract info by going over to this collision parameter.
So I'm going to check it in the bottom.
So I'm going to use this if condition and make first packet and some curly brackets.
And what we want to check over here.
We want to check whatever we triggered it with.
If that game object has a tag called coin.
So let's suppose if we trigger with this coin game object or collide with this coin game object, and
we know we assigned this coin as a coin tag in the inspector, as you can see.
So what we need to do in that situation?
We need to collect this coin game object as well as we need to increase our coin.
So let's go to Skip.
And over here in this brackets we're going to check.
So we're going to type collision dot game object.
So basically we're just taking this parameter collision parameter in order to check and collision dot
game object over here simply referencing the game object we triggered it with.
So we definitely gonna trigger with that uh, coin game object.
So collision dot game object over here is just going to be the coin game object.
And then what we're going to do, we're going to extract the tag of the game object.
So we're going to use this dot tag.
And we're going to check if the tag is equals.
Equals to coin means if we trigger with this coin game object and we do know we assign this coin as
a coin tag means we just collecting that.
So in that situation what we need to do and we're just doing it over here as you can see.
So whatever we trigger it with if that game object has a tag called coin and we're checking it over
here, as you can see.
So if Collision's GameObject or tag if it's equals equals to coin means we just trigger it with that
coin.
So in that situation, what we need to do we need to increase our current coin.
So let's go to top of our class.
And let's create a variable to just keep track of how many coins we collected so far.
And I'm going to do it somewhere in the bottom over here.
And it doesn't need to be public so we can make it instead private.
So I'm going to make it private.
That will be int like whole number 01234.
And I'm going to call this one to current coin current coin.
And let's close that up with semicolon.
So we just created this parameter which is current coin.
In order to keep track of how many coins we collected so far.
And I'm going to move over to this start function.
So start function get call in the start of the game.
So in the start of the game I have what I want to do.
I'm going to set current coin equal to zero.
Why we set it to zero.
Because as soon as we're going to start our game, we're going to have zero coin.
So that's why in the start of the game, I'm just setting current coin equal to zero.
As you can see.
And let's go to that line of code where we're triggering.
So we're triggering over here.
So what are we going to do.
We're going to add current coin plus equal one.
Or what you can simply do you can use this plus plus it also basically the same thing.
So the current coin in the start of the game we have zero.
And if we collide with that coin so what we're doing we just adding one.
So this current coin is zero zero plus one.
Or this plus plus basically means we're just adding one.
So zero plus one is just going to be one.
And if we collect the second coin so the current coin is now one.
So one plus one is going to be two means we will increase our current coin once we trigger with that
coin game object.
And what we also need to do, we need to play the animation clip of that coin collected.
So let's suppose we just collide with this coin game object means instead of playing the spinning of
this coin right now, we need to play the collected, collected.
And we can see we have in this coin sprite the animator component.
So once we created this animation clip, this coin spinning, as well as the coin collected and animator
component just added to this coin sprite.
So basically what we have to do, we need to move over to parent GameObject first.
So in our case coin and by that we can access this coin sprite child game object.
And then we're going to move over to this animator component.
And then we're going to trigger this collide parameter.
And we do know once we're gonna trigger it's going to play the coin collected animation clip right away.
So basically that is the thing we have done the same thing or we just doing in a same way as we have
done already in our hard game object.
So let's do that.
So after increasing our current coin we're gonna play the collected animation clip.
So we're gonna use this collision game object and collision game object over here.
This is gonna be the coin game object.
And then we're gonna move over to Coin game Object Transform component.
And then we're going to use this dot get cell.
And we're going to get the child.
So the we're looking for is a file child or the coin sprite.
So that's why we're going to put zero zero basically means the referencing the first child.
And we have only one child in that coin game object.
So the get child over here zero is gonna be the coin sprite.
So basically we're just going over to that coin sprite game object.
And now we're going to get the animator component.
So we're going to use this Getcomponent function.
Sorry Getcomponent as syntax or this get component.
And in this arrows we need to pass the component name that we really looking for.
So in our case we just need to get the animator component.
So in arrows we're going to pass this animator component and then make first bracket and then dot.
We're going to use this set trigger function in order to trigger that parameter and make first bracket.
Then close that off with Semicolon.
So in this bracket you need to pass the parameter name in quotation.
And the parameter name we're going to pass it called light.
We can see in the inspector sorry we can see in our animator tab which is collide.
So basically what we're doing we're just going over to this coin game object.
And then we're just moving over to this transform component of this coin game object.
And by this transform component which is moving over to this coin sprite game object, as you can see
over here, a coin sprite.
And then after that we're just going over to animator component.
So when we uh moved moved over to this coin sprite, then we're just taking this animator component
as you can see, which is Getcomponent animator.
And then we just, uh, using this trigger function in order to trigger the parameter.
So basically we're just triggering this collide parameter.
And we do know once we trigger this parameter it's going to play the coin collected animation clip right
away.
Uh so we're just triggering the collide parameter as you can see in our code.
Now we're going to press down Ctrl S in order to save our script.
And now let's move over to unity.
So basically we're just moving over to this coin sprite game object and then moving over to this animator
component.
And by this animator component of this coin sprite, we're just getting this collide parameter add.
And we do know once we trigger it's going to play the coin collected animation clip right away.
So let's have a look on this.
And I'm going to click on this play button in the top.
And if I select my player game object uh we can not able to see our current coin because we just made
it private.
Uh, that current coin parameter as a private field.
So let's suppose you still want to see that private field.
So what you need to do in that situation go over to inspector in the top right Inspector you can see
three dots.
So just click there and then click click on this debug.
And as soon as we have chosen debug we can see underneath our player skip in the bottom current coin
and it's set to zero.
So let's click on this play button in the top.
And once we collect this coin game object you will see this current coin just going to increase by one.
So let's have a look on this.
So just take a moment to give it give it a moment to just play that.
So I'm gonna trigger with this coin.
And as soon as we trigger it or collide it.
So I'm going to click on this pause button in the top to pause my game.
And if we move over to player skip we can see current coin is now one.
And as soon as we trigger it, it just plays the coin collected animation clip.
And basically it's just going up and over time it's just fading out.
Uh, so that basically it's just playing perfectly.
But the problem is that if we move over to a hierarchy, we can see the coin game object still laying
around in the hierarchy as well as in the scene view we can see.
So after one second, what we need to do, we need to destroy this.
Why?
Because after one second, we're just completing the whole animation clip.
So after one second, we also need to destroy this coin GameObject, because we collected that one and
we increased our current coin by one unit.
So let's get out of play mode and I'm gonna move over to my inspector mode one more time.
So just go over to top right three dot.
So just click there and then click on this normal in order to go back in normal mode.
So if we select our coin sprite in the hierarchy move over to animation tab and then choose this coin
collected.
We can see after one second the collected animation clip is playing are playing perfectly.
So.
So after one second it's just a fading or it's get totally invisible.
So after this amount of time, we also need to delete the coin game object or the child.
So if we select if we delete or destroy this coin GameObject, other child will also be destroyed with
it because the coin sprite is the child of our coin game object, so that's why.
So let's go to school.
So let's go to script.
So after triggering that parameter we're going to use this destroy function.
And we do not destroy function as a first argument.
It's going to take the game object you want to destroy.
So in our case we're going to pass this collision game object.
It basically means the game the game object we triggered it with.
So we definitely gonna trigger with that coin game object.
So collision game object means over here the coin game object.
And the second thing we're going to specify the time.
So we're going to put one f.
And we do know after one f or once again it's just completing the whole animation clip.
So that's why as a second parameter we just specified our time.
And we're going to close that off with semicolon.
And let's save our script and let's move to unity.
So after one second you will see this coin game object will also be destroyed from the hierarchy as
well as the scene view.
Because we destroying after one second as a second parameter we just specified our time.
So let's click on this play button in the top and have a look on this.
So just wait for to compile.
And let's try to collect this coin game object.
And you will see that.
So as you can see it's played collected.
And if I just pause my game and move over to hierarchy, we cannot see able to see the coin game object
at all in this hierarchy as well as in the scene view.
As you can see, means after once again playing the whole animation clip, which is the collected one,
it just gets destroyed after playing that.
So I hope you get the idea.
So I'm gonna get out of play mode.
And basically that is the thing.
So make sure you just make your coin game object as well a prefab so you can have because we're gonna
place this coin in our ground tiles.
We're gonna have multiple coins in order to collect them.