Using triggers

We can easily configure any collider in Unity to work like a trigger. Triggers are very useful. In this case, we will use them to detect whether our character has fallen into the hole. I have already prepared another useful prefab for you, so we won't waste any time setting it up. The steps are as follows:

  1. Import KillTrigger.unitypackage into your project.
  2. Drag Kill trigger into your project.
  3. Position the KillTrigger game object so the red area is below the ground.
    Using triggers

This is all we need in the Scene view. Once Jake drops down from the end of the platform, he will most certainly fall through the red trigger zone. Now, we need to write some code to describe this behavior. It will be a very simple component added to the KillTrigger game object.

Create new a C# script, call it KillTrigger, and write the code so it looks like this:

Using triggers

As you can see, there is nothing complicated here. We use the OnTriggerEnter2D method. It is called automatically by Unity whenever another 2D collider enters the trigger area.

Before we test how things work, we need to make sure out Player game object has the correct tag set up. Unity uses tags to help you recognize the game objects in code. It is very useful and easy to use. Select the Player game object and set its tag to Player:

Using triggers

Now, we are ready to perform a test. Press Play in Unity. Then, press S on the keyboard to start the game. Our character will start running. If everything works properly, we should get the Console message as soon as the character touches the trigger area.

Nice! We can trigger the parts of the code by physics events. Think about different things we can use triggers for, such as collecting stuff! Anyways, at the moment we just call the Debug.Log. What we really want here is an actual functionality.

Simple logic says if player touched the killTrigger, player should die. Let's go back to PlayerController and add a new method in.

public void Kill() {

  GameManager.instance.GameOver();
  animator.SetBool("isAlive", false);
}

Before we call this method in the PlayerController, we need to change PlayerController class itself into a singleton. We have done this already with GameManager. Go ahead and add the instance static variable to PlayerController and assign it in the Awake method. If you feel a bit lost now, go back to the previous part of this chapter where we learned about the singleton approach.

Now, we have a really easy Kill method to call when something bad happens to our character. Let's go back to the trigger killer and call it instead of Debug.Log(). After editing the Kill trigger class should look like this:

Using triggers

Test the newly written or edited code as soon as possible. Select GameManager in the Hierarchy first. Press Play in Unity and then press the S key to start the game. As soon as our character falls into the trigger area, GameManager.currentGameState should change from inGame to game over.

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

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