Creating Triggers

In this tutorial, you will create Collectible coins with Triggers

Triggers



Open 3D World Building project you have already created. You will be adding to that world in this lesson.

Triggers are colliders that ignore the physics engine in unity, which means that colliding with them will have no impact on your movement. They are used in a variety of ways like creating an invisible area that triggers a cutscene, or when collecting coins and power-ups.

Let's create an object with a trigger. First create a 3D cube and place it near your player. In the Inspector there is a Component named Box Collider. In the Box Collider check the box that says Is Trigger.



You can see that after adding the Trigger to the object the player can now walk through it. The Trigger still detects what is currently touching it, that way we can see what objects or players enter the Trigger.

Collectibles



Using the Trigger, we can create a collectible the player can pick up, like a coin or experience points (XP).

Let's create a simple script so we can collect coins for the player. We're going to create our own custom script using code, but we will give you all of the code to copy and paste into the script if you don't already know how to program.

First, go to the Scripts folder, and right-click to bring up the menu. Then, go to Create > C# Script. Name the script you have just created CoinScript.

Make sure that you name it exactly CoinScript. If you name it anything else, the code provided in this example won't work.

Double-click the created file in the project window to open the file in Visual Studio.

Copy the code below, and paste it into the script, replacing all of the text that is there.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoinScript : MonoBehaviour
{
  private void OnTriggerEnter(Collider other)
    {
        //Add 1 to coins
        other.GetComponent<FirstPersonController>().coins++;
 
       //Destroys the coin after collected
        Destroy(gameObject);
    }

    void Update()
    {
      //Rotates the coin 
        transform.Rotate(0, 0, 90 * Time.deltaTime);
    }
}
          


Save the file by pressing Ctrl + S, or going to File > Save, then return to Unity and the script will be updated.



Now that we have a script, we need to create an object to put the script on.

Create an object you want to be your coin. One option is to create a cylinder and resize it to be flat. After that, check the Is Trigger box under its collider, and name the object Coin.

Add the new script CoinScript to the object by clicking the Add Component button, and typing in the name of your script (CoinScript). You will see the script appear in the selection list, and once you click the script it will be added to the object.

Now, when you run into the coin, it will disappear and you will be able to see the Coins variable update in the First Person Controller script.



We need to make a lot of coins for the game, so instead of re-creating the coin from scratch again we can make it a Prefab. Prefabs are Game Objects that save all of its components, values, child objects. That way, you can make one coin and then place the coin's prefabs around the map.

Create a folder named Prefabs. Then, drag the coin from the hierarchy into the folder. Once it is in the folder it becomes a prefab, and the object's name in the hierarchy will turn blue. Now you can drag as many coins as you want into the scene, and they all will be the exact same as the original.



Another benefit of a prefab is if you need to make any edits to the object, it will apply it to all the placed prefabs in the scene.

We can add a color to our coins to make them stand out.

First, right-click in the Project window and go to Create > Material to create a new material object, and call it Coin. Then, click on the object you created if it does not already show in the Inspector Window.

In the inspector window, go to the Albedo field and pick the color you want the coins to be.

Then, click on your prefab object, and in the inspector window click Open Prefab, which will open the Prefab Editor. In the Prefab Editor, drag the material onto the coin in the editor. After that, press the back button in the Hierarchy window to exit back into the scene.

When you look at your scene, you will see that all of your coins' colors have been updated!



Prefabs are the main way that you, as a designer, will create games inside of Unity in this course. In many future projects, you will be given the prefabs that you need to set up a game of a particular type, and you will be arranging those prefabs inside the scene in ways that are interesting for the player.

Prefabs don't just apply to coins and collectibles, any object in the game can become a prefab. Creating prefabs out of objects you will reuse again and again can greatly speed up your game design process.