To make things a little easier, I have already prepared the visual part of our coin. Download Coin.unitypackage
and import it into your project:
Great job! Now we drag the
Coin prefab into the Hierarchy view so that we can take a look at it. As mentioned before, I prepared this prefab visually. I have added Sprite
Renderer
to the game object and linked it with the coin sprite. I have also created a simple spin animation controlled by the Animator. Don't worry about it right now.
Now, you have to pick up from where I finished. Most collectables react with the environment through the physics of the game. We can use the 2D trigger here to react with the Player
Game Object Rigidbody2D
component. This is the exactly the same way we inserted LeaveTriggers
into the game previously.
Select the coin and add the CircleCollider2D
component. A green circle will appear around the coin, representing the triggering area. Make sure you tweak the radius value to roughly match the size of the coin. In my case, a value of 0.35 works great.
Another requirement that we need to fulfill to make the trigger work is ticking the IsTrigger
checkbox. That's it! Our coin is ready to process trigger events. All that we need to do now is write some code to manage its behavior:
Let's plan what behavior we want from our collectable. We will need the following methods:
Show()
: This will show the coin and activate its colliderHide()
: This will hide the coin and deactivate its colliderCollect()
: This is called at the moment of collection of the coinOnTriggerEnter2D
(Collider2D other): This is called by Unity's physics system when the collider enters our coin's triggerCreate a new C# script, call it Collectable
, and write the following code:
Add the Collectable
script to your coin
prefab. We are ready to test it now! Make the coin
prefab a child of one of the level chunks. Create a few copies of the coin next to each other so that you can test them better.
Let's click on Play in Unity and see what happens. You should notice that the coin disappears upon contact with a Player
Game Object. That's exactly what we wanted. Well done!
The last thing to do now is counting the collected coins. We will store the number of coins collected in the
GameManager
class and increment the int
number every time a coin is collected. Let's add some functionality to the GameManager
class:
collectedCoins
public member:public int collectedCoins = 0;
public
method that you can call when a coin is collected:public void CollectedCoin() { collectedCoins ++; }
What this method does is increment the collectedCoins
variable by 1
every time the ++
operator is used.
Collectable
class, add a single line calling CollectedCoin()
from the Collect
method, as follows:Now click on Play in Unity. Let's take a quick look to check whether it works. Select GameManager and press the Play button. You will notice that the number of collected coins in the Unity Inspector increases every time a new coin is collected. Great! We now know that our collectedCoin
variable works. However, we still need to update the label value in the gameView
so that our user can see exactly how many coins they have collected.
It's a good idea to keep UI-controlling code separate from the GameManager
. Let's write a short script that controls the UI on the Game View. Create and add this component to the InGameCanvas
game object:
Once you have the View In Game component added, drag the coinLabel
game object into the coinsLabel slot:
The View In Game component should look like this:
What our code does is update the .text
string value on the label. By doing this, the text on the UI changes and is always up-to-date with the collectedCoins
value in Game Manager.