Game Development Reference
In-Depth Information
Figure 5-45. The Cube reactivated and blocking the doorway
6.
In the script, change the code to filter for the tag:
void OnCollisionEnter (Collision theVictim) {
if (theVictim.gameObject.tag != "Ignore") {
print (theVictim.gameObject.name + " got hit");
}
}
Now the message will print only if the hit object does not (!=) have the Ignore tag.
7.
Save the script.
8.
Set the bench's Speed back to 0.5 .
9.
Click Play, and watch the console.
This time, only the Cube is reported as being hit.
Creating User-Defined Functions
You may wonder why the bench is not moving the Cube. The reason is that the Cube does not have
a Rigidbody component but does contain a collider. Once the bench runs into the Cube, it is no
longer able to go forward even though its code is telling it to do so.
Ideally, it would be more efficient to turn off the code that tells the bench to go forward when it
can no longer go forward. The great thing is that all you will have to do is set the allClear flag
back to false . But that will mean contacting the other script. That is the essence of interaction in
Unity: the communication between scripts, whether they are on the same gameObject or on other
gameObjects.
 
Search WWH ::




Custom Search