Pickup

Pickup tutorial teaches us how to make game objects that players can pick up.

Preparation

You should do the Trigger tutorial before doing this one if you haven’t already. You can do this tutorial to add onto the basic FPS you made in the FPS tutorial..

Coin Collecting

This tutorial will teach you how to make a simple game with a coin that will disappear when your player touches it, just like in Super Mario Bros.

First you need to set up your project in Unity:

  1. Open Unity on your computer.
  2. After the Project window pops up, click the New button to create a new project.
  3. In the Project Name field, replace “New Unity Project” with “Coin Collector”.
  4. Click the Asset Package button.
  5. Another window will pop up. Click the Characters asset package, then click the Done button.
  6. Click the Create Project button.
  7. Wait for Unity to load your project.

Setting up 3D Models

Now that your project is set up, you need to create the 3D objects that will make up your game. You will make a ground, a coin, and set up your player.

Ground:

  1. In the Hierarchy window, click the Create button. Hover your mouse over 3D Objects, then click Plane.
  2. In the Inspector window, click the little gear in the top right corner on the Transform component, then click Reset. It’s good practice the reset a game object after creating it.
  3. In the Hierarchy window, right-click Plane, then click Rename. Rename the Plane to Ground.

Coin

  1. In the Hierarchy window, click the Create button. Hover your mouse over 3D Objects, then click Cylinder.
  2. In the Inspector window, click the little gear in the top right corner on the Transform component, then click Reset.
  3. In the Hierarchy window, right-click Cylinder, then click Rename. Rename the Cylinder to Coin.
  4. In the Inspector window, set the Y and Z Position to 4 in the Transform component.
  5. Also in the Transform component, set the Y scale to 0.1.
  6. Then set the X Rotation to 90 and the Y Rotation to 45

Player

Your game will have a first person view, so you don’t have to make a 3D Object for the player. Instead you will give the Main Camera a Character Controller and a Collider.

  1. In the Hierarchy window, right-click Main Camera, then click Rename. Rename the Main Camera to Player.
  2. In the Inspector window, click the little gear in the top right corner on the Transform component, then click Reset.
  3. In the Inspector windows, set the Y Position to 1 in the Transform component.

Setting up Player Input and Collision

Now you need to give your Player the ability to move around in your game. You will also give your Player a Collider, which will allow your player to interact with other 3D Objects.

  1. In the Project window, click the search bar, then type FPSController
  2. Drag the FPSController prefab to Player in the Hierarchy window. FPSController is a collection of scripts that comes with Unity.
  3. The FPSController prefab should now be a Sub-Object under Player in the Hierarchy window.
  4. In the Hierarchy window, Click the FPSController prefab under Player.
  5. In the Inspector window, click the little gear in the top right corner on the Transform component, then click Reset.
  6. In the Hierarchy window, click the Player object.
  7. In the Inspector window, click the Add Component button.
  8. A new window will pop up. Click Physics, then click Sphere Collider.
  9. Click the Play button above the Scene view. At this point, your game should be playable. Use the mouse to rotate your Player’s view. Press the W, A, S, D keys to move your Player. And press the spacebar to jump.
  10. When you are done making sure it works, click the Play button to stop the game.

Adding Color to the 3D Objects

  1. In the Project window, click Create, then click Material.
  2. Go back to the Project window and create another Material.
  3. Click New Material, then click the small white box in the Inspector window. It’s under Main Maps, to the right of the word “Albedo”.
  4. A new window will pop up. Mess around until you see the color green.
  5. Exit out of the window.
  6. In the Project window, drag New Material to Ground in the Hierarchy window.
  7. In the Project window, click New Material 1, then click the small white box in the Inspector window.
  8. Make a yellow color, then click exit out of the color picker.
  9. In the Project window, drag New Material 1 to Coin in the Hierarchy window.
  10. At this point, the Coin should be yellow and the Ground should be green.

Writing a Coin Collector script

Now you need to write a script that makes the coin disappear when the player touches it.

  1. In the Project window, click the Create button, then click C# Script.
  2. In the Project window, double-click NewBehaviourScript
  3. MonoDevelop is a separate program that you will use to write your script. Wait for it to open.
  4. In your script you will use a piece of code that will make the Coin disappear when the Player Collider touches the Coin Collider.
  5. Copy the code below and paste it between the two curly braces at the end of the script in MonoDevelop.
void OnTriggerEnter(Collider other) 
{
    if (other.tag == "Player")
    {
       transform.GetComponent<Collider>().enabled = false;
       transform.GetComponent<Renderer>().enabled = false;
    }
}
  1. This code uses a method called OnTriggerEnter to disable and hide the Coin when the Coin’s collider touches the collider of another object.
  2. Press Ctrl or Command and S at the same time to save the script.
  3. Go back to Unity.
  4. In the Project window, drag NewBehaviorScript to Coin in the Hierarchy window.
  5. In the Hierarchy window, click Coin.
  6. In the Inspector window, find the Capsule Collider component, then click the box next to Is Trigger. You have to do this to make the coin an object that can trigger an event.

Play!!!

  1. Click the Play button on top.
  2. Collect the Coin by jumping and touching it.
  3. After the Player touches the Coin, the Coin will disappear in both the Game window and the Hierarchy window.

Troubleshooting

If your coin doesn’t work:

  • Make sure that your player character game object (probably called FPSCharacterController) says Tag is Player in the Inspector.
  • If you get syntax errors in the console, double-check that you copied the code exactly with all capitalization the same.

Improvements

There are a ton of ways to improve this basic game.

Sound Effects

  • You could trigger a noise when the Player touches the Coin.
    1. Select the Coin object.
    2. Click AddComponent and click Audio.
    3. Add an AudioSource to the pickup.
    4. Click here to download a sample coin sound.
    5. Drag the downloaded sound file from your Downloads folder to the Project panel to copy it into your assets.
    6. Assign the audio clip of the AudioSource by dragging the audio file from the Project panel into the box.
    7. Make sure the AudioSource is set with play on start unchecked.
    8. Set spatial blend to 1 to make sure the sound will be positional.
  • You could make a 3D model for the Player, and move the camera back to make the game in 3rd person.

Inventory tracking with variables

You could make a counter that increases by 1 every time the player collects a coin.

Add a static variable to the Coin.cs script to track the total coins found like this:

 

public static int coins = 0;

In the void OnTriggerEnter( Collider other) code block, add a line so it looks like this:

void OnTriggerEnter(Collider other) 
{
    if (other.tag == "Player")
    {
        transform.GetComponent<Collider>().enabled = false;
        transform.GetComponent<Renderer>().enabled = false;
        coins++;
    }
}