Hello, guys.
In this video, we're going to make a coin for our game.
It's going to be very simple task, honestly.
And we're going to begin with adding graphics to the coin.
We already have an object in the game, this one, but it's empty.
So what we can do is to add Sprite Renderer.
Sprite renderer.
Oh, wait a second.
Mouse highlight.
So we have Sprite Renderer.
Now we can go to graphics, find coin.
Make sure we have pixels per unit 16.
All right, now let's select coin and drag Sprite over here.
Now we need to change certain layer and we can choose collectible.
All right.
Now, let's close this.
We can go to assets animations.
I'm going to create a folder for player animations layer.
Then I'm going to drag all of this inside and I can create new animator controller.
Over here going a C.
Let's drag it to the coin like that.
Then we can go to animation window and create new clip that's going to be just coin idle.
We're not going to have any other animations for COIN, but it's kind of a good practice to give an
understandable name.
Let's go to graphics now and just take all of the sprites of the coin and drag it inside to coin idle.
And I want to change sample rate to something like six.
Let's see.
Can this.
I also think we could make this coin a bit smaller because it's kind of bigger than even the head of
the player.
So we can do maybe.
Point eight.
Let's see.
I think it's okay.
If not, we can change it later.
So now we have a coin.
Now let's go to a script of the coin, and I'm going to explain how it's going to work.
So we can delete this.
And we need one function that you've seen already in this project.
And if you have some experience with unity, you've probably seen this function before and you used
it on trigger enter two D, So on trigger enter is called whenever something enters the trigger.
Now how do you create the trigger?
Also, very simple.
Let's go back to Unity and add Collider.
I'm going to choose Box Collider.
I know it's a coin and we could use Circle Collider, but I think it's nothing bad if you make Collider
a bit bigger than the coin is, so it's easier to pick up for the player.
So we kind of help the player without even him knowing it.
So let's adjust the size.
Just approximately bigger.
Nothing special.
And then we can check.
This box is trigger, which is important.
Click.
Now it is a trigger.
And now whenever something enters the trigger, we can do something again.
But I want to do something specific because at the moment, anything that will enter the trigger will
trigger it.
But we need to be sure that it's triggered only when player enters the trigger.
There are a couple of options to do that.
First, I'm going to show you what mostly people use.
It's a tag comparing so you can do.
If collision collision means the object that collided, the tag equals equals two and you give a name
of a tag, for example player.
Then debug log.
Coin.
Right.
How you assign a tag, you're going back to unity.
Select the object tag and you can choose player here you can see or you can add another new one that
you want.
And it's going to work.
It's going to work for sure, But I want to use a different way.
And just make sure it's working.
So I'm entering the trigger boom coin.
All right.
Let's go back.
I want to use component because I.
This is not my first course.
And even this course.
Exactly.
I'm reworking to make it better.
I'm trying to and I've had issue with the students who couldn't get the name right, like they could
use small p or mix sub the name, misspell whatever, and it causes some confusion and frustration situations,
I would say.
So what I want to do here is not compare in a tag, but to check if object has a component which is
as easy to do as this one.
We can do collision dot get component.
So we check if collision get component of player not equals to null.
Which means there is a player component, which means it is a player.
Then we can do some action and instead of debug this time, let's do game manager dot instance, dot
coins plus plus.
And right after we added a coin, we can destroy the coin as well.
So destroy the game object and it's going to destroy a game object with this script.
And let's go and test this.
I'm going to save the script, go back to Unity.
So my game manager has zero coins.
I'll place coin in a way and I'll try to pick it up.
Boom coin was picked up, It was destroyed.
And my game manager has one coin now.
Perfect.
Now let's select Project folder prefab and drag this coin over here.
After you did this, you can delete Coyne from the hierarchy, and whenever you need to place a new
coin, you can do it just like this or you can place it on a level part.
You can choose level part and just drag coin over here and it's going to be there whenever you instantiate
new level bar.
But we're not going to do it like that.
We're going to create a coin generator right in the next video.
So let's go and do that.