Skip to Content
Mobile Game Development with Unity
book

Mobile Game Development with Unity

by Jonathon Manning, Paris Buttfield-Addison
August 2017
Beginner
464 pages
9h 18m
English
O'Reilly Media, Inc.
Content preview from Mobile Game Development with Unity

Chapter 6. Building Gameplay with Traps and Objectives

Now that the foundations of the gameplay are set up, we can start adding in game elements like traps and treasure. From that point, much of the rest of the game is simply level design.

Simple Traps

Most of this game is about when the player hits things—traps, the treasure, the exit, and so on. Because detecting when the player hits certain objects is so important, we’ll create a generic script that triggers a Unity Event when any object that’s tagged with “Player” collides with it. This event can then be set up in different ways for different objects: the traps can be configured to tell the Game Manager that the gnome has received damage, the treasure can be configured to tell the Game Manager that the gnome has collected treasure, and the exit can be configured to tell the Game Manager that the gnome has reached the exit.

Now, create a new C# script called SignalOnTouch.cs, and add the following code to it:

  using UnityEngine.Events;

  // Invokes a UnityEvent when the Player collides with this
  // object.
  [RequireComponent (typeof(Collider2D))]
  public class SignalOnTouch : MonoBehaviour {

    // The UnityEvent to run when we collide.
    // Attach methods to run in the editor.
    public UnityEvent onTouch;

    // If true, attempt to play an AudioSource when we collide.
    public bool playAudioOnTouch = true;

    // When we enter a trigger area, call SendSignal.
    void OnTriggerEnter2D(Collider2D collider) {
      SendSignal (collider.gameObject);
    }

    // ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Holistic Mobile Game Development with Unity

Holistic Mobile Game Development with Unity

Penny de Byl

Publisher Resources

ISBN: 9781491944738Purchase bookErrata Page