Game Development Reference
In-Depth Information
Triggers
The physics engine automatically adjusts the position and rotation of game objects according to the
physical characteristics you assign to them. The OnCollision() function calls allow you to direct
what happens at the beginning, during, and at the end of the collision. There are many scenarios
in games where you want to direct the game action when one game object reaches a particular
location. It may be for a door to automatically open as your game object approaches, to spring a
trap as your game object travels through the doorway, or perhaps to end the scene when your game
object leaves the doorway and enters the room. At no point does your game object actually collide
with a physical object, so in these scenarios you would use a trigger collider.
Note Best practices call for the game object with the trigger collider to be a static object. The game object
with the Rigidbody component that is governed by the physics engine passes through the trigger collider.
Is Trigger is a property of the Collider component. When the Is Trigger box is checked, the collider
will detect a collision but call a different series of methods: OnTriggerEnter() , OnTriggerStay() ,
and OnTriggerExit() . No physical collision occurs with the rigidbody passing through the volume of
the trigger collider. You use these methods much like OnCollisionEnter() , OnCollisionStay() , and
OnCollisionExit() .
OnTrigger
In the Project panel Assets ➤ Scene folder, create a new scene, name it TriggerTest, and save it. Add
a directional light, and a Plane with a transform position of (0, 0, -5). Create a Sphere game object
with a Transform position of (0, 0.5, -7). You are going to push the Sphere game object so that it rolls
through the trigger collider. From your Scripts folder in the Project panel, drag the Push script and
drop it onto the Sphere game object. A game object must have a rigidbody for physical forces to act
on it, so select the Sphere game object in the Hierarchy and in the Inspector select Add Component
➤ Physics ➤ Rigidbody.
Back in the Hierarchy, create a Cube game object and name it Trigger. Give it a Transform position of
(0, 0.5, -3) and a Transform scale of (3, 3, 1). Now set up a couple of goalposts on either side of the
Trigger game object by creating a Cube game object named Cube1, with a Transform scale of (1, 3, 1).
Give Cube1 a Transform position of (2, 0.5, -3). Duplicate it by selecting Edit ➤ Duplicate in the top
menu or pressing +D, name the new game object Cube2, and give it a Transform position of
(-2, 0.5, -3).
Save the scene and playtest. Give the sphere a push and you'll see that it bounces off of the
Trigger game object. Remember that only one object needs to have a rigidbody for a collision to be
detected. Exit Play mode.
Select the Trigger game object and, in the Inspector, check Is Trigger in the Box Collider component.
Save and playtest. This time, no physical reaction occurs and the sphere rolls right through the
Trigger game object after you push it.
 
 
Search WWH ::




Custom Search