Game Development Reference
In-Depth Information
Inside the
OnCollisionEnter
function, under the
print
statement, add the
following:
8.
gameObject.SendMessage("ToggleStopIt", true);
9.
Save the script.
10.
Click Play.
11.
Switch to the Scene view, and select the Cube.
12.
When the bench stops at it, move it up clear of the bench.
13.
Do not exit Play mode yet.
The bench does not move forward when the Cube is moved out of the way.
14.
Now select the bench in the Hierarchy view.
15.
In the Inspector, toggle the Stop It parameter off.
The bench moves forward once again and goes over the edge.
As a last little test, you can try the
OnTriggerExit
function. You may remember the Is Trigger
parameter in the collider components. It lets the object detect intersection without functioning as
a barrier. Because the script stops the forward movement, let's use
OnTriggerExit()
instead of
OnTriggerEnter()
. That way, you can see the bench going through the collider instead of being
blocked by it at first touch.
In the ColliderTests script, copy the entire
OnCollisionEnter
block of code
and paste it below the original.
1.
2.
Modify the first line as follows:
void OnTriggerExit (Collider theVictim) {
Note the
Collider
type for the argument instead of
Collision
.
3.
Save the script.
4.
Select the Cube.
5.
Check Is Trigger in its Box Collider component.
6.
Disable its Mesh Renderer.
7.
Click Play, and turn on Gizmos on the right side of the Game view's tool bar.
The bench trundles through the Cube's collider and stops just as it starts to leave on the other side
(Figure
5-46
).