Game Development Reference
In-Depth Information
What does the script simply do is to verify that owner has an inventory box. If this is true,
it adds the secret key stored in key variable to the list of keys in the inventory box ( box.keys ).
Finally, the collectable key is destroyed and removed from the scene, which is important
to give the player the impression that he has already collected the key. The question now
is: how the player is going to collect the key? The answer might vary depending on the
situation: he might simple pick it from the floor, or it can be given to him by another char-
acter in the game, and so on. Generally, any event that ends by sending Collect message to
key object and providing player's character as owner will eventually give the player the key.
In our case, we simply collide with the key object and collect it. Consequently, we need a
script that sends Collect message upon collision between the player and the key. This script
is CollisionCollector shown in Listing 63.
1. using UnityEngine;
2. using System.Collections; 3.
4. public class CollisionCollector : MonoBehaviour { 5.
6.
void Start () { 7.
8.
}
9.
10.
void Update () { 11.
12.
}
13.
14.
//Collect the collectable on collision
15.
void OnCollisionEnter(Collision col){
16.
SendCollectMessage(col.gameObject);
17.
}
18.
19.
//Collict on trigger hit
20.
void OnTriggerEnter(Collider col){
21.
SendCollectMessage(col.gameObject);
22.
}
23.
24.
void SendCollectMessage(GameObject target){
25.
//Send collect message to the colliding object.
26.
//Provide self as owner of what is to be collected
27.
target.gameObject.SendMessage("Collect",
Search WWH ::




Custom Search