Creating the coin pickup

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:

  1. To start with, navigate to the Assets folder, right-click on it, and select Create Folder. Name this folder Pickup.
  2. Open the 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.
  3. With the Pickup_Coin.png file imported, select it. Then, in the Inspector window, select Sprite Mode as Multiple.
  4. Then, select Filter Mode as Point.
  5. Now, select Max Size as 256.
  6. Then, select Format as Truecolor.
  7. Finally, click on Apply.
  8. Now, click on the Sprite Editor button to open Sprite Editor.
  9. Then, click on the Slice button in Sprite Editor and select Type as Grid.
  10. Now, select Pixel Size for both X and Y as 32.
  11. Then, select Pivot as Top Left.
  12. Now, click on Slice and then on Apply.
  13. Select the Pickup_Coin.png file from the Assets/Pickup folder and drag it onto the Scene window.
  14. A new window will open to create an animation file for it; this is similar to what we did for the character running and jumping.
  15. Name this file Coin_Spin.
  16. Double-click on the Pickup_Coin_0.controller file to open Animator.
  17. Then, right-click on the Any State node in the Animator window and select Make Transition.
  18. Now, left-click on the Coin_Spin node to connect the nodes.
  19. Select the Pickup_Coin_0 GameObject in the Hierarchy tab.
  20. In the Inspector tab, select Add Component.
  21. Search for Box Collider 2D and select Box Collider 2D.
  22. In the Box Collider 2D component settings, check IsTrigger.
  23. Change the size of Box Collider 2D from X as 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.

  24. Right-click on the Assets/Scripts folder and select Create and C# Class.
  25. Name this C# Class as Coin.
  26. Then, click on the Pickup_Coint_0 GameObject in the Hierarchy tab and select Add Component.
  27. Search for Coin and select Coin.
  28. Double-click on the 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:

Creating the coin pickup

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:

Creating the coin pickup

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.

Tip

gameObject (lowercase g in game) is a reference to GameObject that the C# class belongs to.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset