Unity Triggers

Posted on: January 20th, 2015 by tdc_user
Unity Triggers are initiated when the player (or some other game object) enters the collision area of another game object which has the collider set to “Is Trigger”. Unity Triggers example 1: Start with a standard terrain or you can even use a big resized box as a platform if you want. That’s what I’ve done here:
a concrete platform

A starting platform. Click on image to see larger version.

Next we will need a first person controller so we have a way to interact with other game objects. When I started this project I checked the box for first person controller, but you can always do this – Assets > Import Package > Character Controller. The First Person Controller can be found under Assets > Standard Assets > Character Controllers. Use the Move tool in the upper left corner to reposition the character controller above the ground. Rename this game object to Player, and choose Player as the tag for this object as well. All of this has been done in the following screenshot:
the-first-person-controller-positioned-and-named

Setting up the first person controller. Click on image to see larger version.

Create a Unity cube and name it TriggerCube. Position it in front of your Player. Every default cube has a Box Collider – so increase the size of the Box Collider so that it is about twice as big as the cube. Check the box Is Trigger. Now let’s just send a message to the console when the player has entered the trigger area of TriggerCube. Here is the script we will want to attach to TriggerCube. Create a new Javascript script named “TriggerExample.js”.
the-new-trigger-script

Creating a new trigger script. Click on image to see larger version.

Replace the default code with this code: Now start the game, walk your First Person Controller towards TriggerCube until you bump into it. You will see the Debug.Log message sent to you at the bottom of your screen, via the Console.
trigger-works

The Player collides with the TriggerCube’s box collider and causes a message to be sent to the Console. Click on image to see larger version.

Now create a Unity Cylinder and move it to a position near the TriggerCube. If we add one line to our code, we can destroy the Cylinder when we enter the trigger area: Application: Let’s imagine have two unity cylinders which have been renamed RampSupport and RampSupport2. Upon them lies a resized box named Ramp. If we destroy RampSupport, Ramp will fall. If we have a TriggerCube that has its Mesh Renderer turned off (unchecked), the player will not see it. He or she will simply walk in some random spot and suddenly a ramp or bridge will begin to collapse. This kind of random action in a game can add depth, surprise, danger, etc. Related Tutorial – Unity Collisions See a related tutorial on how to use a Unity Collision Trigger so that objects which have been hit by a “bullet” or other collider can be destroyed, modified, or used to trigger other events.

Comments are closed.