Game Development Reference
In-Depth Information
The mechanism we are going to use is the following: each key has a “secret text” that must
be unique, and this text can be used to open all doors that are locked with the same secret
text. The list
keys
in
InventoryBox
stores secret texts of the keys that player currently has.
Now we have to 1) create a collectable key that gives the player the secret text, and 2) cre-
ate a lock that prevents door from opening until the secret text is provided by the player.
Let's begin with the collectable key: we need to build a new collectable/collector mechan-
ism, but this time we are going to make use of collision detection. Therefore, we do not
need to iterate over all collectables in the scene and measure their distances like we did
in section 3.2. Alternatively, we simply create a key script that responds to
Collect
mes-
sage by giving the collector (owner) a new key in a form of secret text. Listing 62 shows
CollectableKey
script which implements the described function.