The coin pickup is a representation of the good type of collider the character can interact with. It will be what the player collects in the game that they can use for currency. Perform the following steps:
Assets
folder, right-click on it, and select Create Folder. Name this folder Pickup
.Pickup
folder, right-click on it, and select Import New Asset…. Navigate to the Art
files for this book, and open the ChapterThree_Pickup
folder, and select the Pickup_Coin.png
file to import.Pickup_Coin.png
file imported, select it. Then, in the Inspector window, select Sprite Mode as Multiple.256
.32
.Pickup_Coin.png
file from the Assets/Pickup
folder and drag it onto the Scene window.Coin_Spin
.Pickup_Coin_0.controller
file to open Animator.0.32
and Y as 0.32
to X as 0.2
and Y as 0.2
.You will see that these steps are similar to the steps we took for the character and just like the character, this pickup will need a code class for itself, although it will be much smaller than the character.
Assets/Scripts
folder and select Create and C# Class.Coin
.Coin.cs
file to open it.Just as with all the other classes, you will find only the generated code from Unity, including the Start
and Update functions and two Includes
:
Under the Update
function, add the following code:
// When trigger collider is collided with void OnTriggerEnter2D(Collider2D Col) { print( Col.gameObject ); }
If you position Pickup_Coin_0
before the character and play the game and jump on the coin you should see the following console message:
Unlike the collider on the character, the trigger collider will not block the colliding object from moving; it will only say that something has collided with it. This is handy for us because we don't want the coins to block the character if it hits the coin. We only want to know that the character has collided with it. OnTriggerEnter2D
is a Unity function that is similar to the OnCollisionEnter2D
function we used in character. It will give us the Collider2D
object that it has collided with so that we can use it to do something, that is, give the character one more coin in our case.
Update the OntriggerEnter2D
function as follows:
// When trigger collider is collided with void OnTriggerEnter2D(Collider2D Col) { Character gameChar; if (Col.gameObject.name == "Character") { gameChar = Col.gameObject.GetComponent<Character>( ); if (gameChar != null) { if (!gameChar.isDead) { gameChar.CoinCount += 1; print(gameChar.CoinCount); Destroy(gameObject); } } } }
This function will first check whether gameObject
who owns Collider2D
, which is being passed to the function, is named Character
. As there will only be one GameObject named this, we will know that this is our character.
We will then assign a local reference to the Character
component of the Character GameObject and check to make sure that it exists by verifying that it is not null.
If the character is not null, we will then make sure that it is not dead because we don't want the character to pick up anything when it is dead.
Once we have a valid reference to the Character
component of the Character GameObject and know that the character is not dead, we can then add one to the CoinCount of the character and print out the current value of coins held by the character.
Once we have given character a coin, we will no longer want this coin to do anything because it's already been used, so we can call the Destroy
function and give it the gameObject
of the coin to destroy, removing it from the scene and preventing character from touching and collecting another coin from it.
This is our pickup class. This class is designed to only perform one thing—add one to a value of another class—so it is a simple C# class to write.