Thanks to the Inventory Engine, the TopDown Engine natively supports the creation, collection and use of keys. Keys are inventory items that, combined with a few components on your scene elements, will allow you to interact with doors, open chests, loot their contents, and generally activate stuff in your scenes. If you want examples in action, you can check (among others) the MinimalSandbox2D demo scene, located in Demos/Minimal2D, or the KoalaDungeon scene, they’ll provide working examples of key activated doors and chests.
Creating a key
If you’re familiar with the Inventory Engine, this will be easy. If not, you should probably check out its documentation. To create a key, simply right click in one of your Resources folder, then go Create > More Mountains > TopDown Engine > InventoryEngineKey. Give this newly created scriptable object instance a name, and you’re ready to fill its details in its inspector.
The key’s inspector is the same as any Base Item you can create in the Inventory Engine, so we won’t go over the details again here, but note that the Key’s name (ItemID) is really important, as it’s the string you’ll use to say what key can open what door/chest/etc, so it needs to be unique.
Once you’ve created that scriptable object instance, all that’s left to do is to create a pickable item for it. If you don’t remember how that’s done, check out this documentation.
Creating a door
One of the most common things you can open with a key is a door. So let’s start with that. The most simple way to create an openable door in the TopDown Engine is to reuse either the 2D or 3D door prefabs. It could also be a moving platform, or a wall, or anything you want. The only mandatory element will be a Key Operated Zone component.
Key Operated Zones
Once you’ve got your moving door/platform, you need a zone to interact with the key and open it. For that, create an empty game object, add a Box Collider 2D to it, set it to IsTrigger, resize it to your liking and position it somewhere between your key picker item and your door. Then, add a KeyOperatedZone component to it.
KeyOperatedZones are an extension of the ButtonActivated class, which means they’ll combine the advantages and options of ButtonActivated elements (dialogue zones for example) and add key support to them.
From your zone’s inspector, you can set, among other things, whether the zone is auto activated or not (in this case it’ll require you to press a button). You can also specify a number of activations, and a delay between each use. If your zone is not auto activated, you can decide to have a button prompt show up always or only when when colliding, and where it should appear. Finally, you can decide that this zone requires a key, and in that case what KeyID the zone should look for in your character’s inventory.
The last thing to do is to bind an action to this zone. This action is a method on any object in your scene that will be triggered when the zone is activated (when you enter it and have met the conditions you just defined to activate it : button pressed, key in inventory, etc). In the case of the 2D door, you’d have to drag the DungeonDoor component into our action field, and select its ToggleDoor method.
With this simple component, you can control doors, but really any object with a public method bindable to this Key Action field. You can have doors that open when you approach, or when you press a button, or that won’t open without a key, etc.
Creating a door that requires a key to open
In this example we’ll create a “door” (technically a cube that disappears), and we’ll associate it to an interactive zone that will require a key to open.
- fresh install of TDE v2.1 on Unity 2019.4.28f1
- open Loft3D demo scene
Creating the door
- create a new Cube, name it “Door”, position it at -15,0.5,0, set its scale to 1,5,1
- create a new Cube, name it “DoorZone”, set its BoxCollider to trigger, position it at -14,0,0
- add a KeyOperatedZone to the DoorZone object, add an event to OnActivation, drag the Door into its slot, and select GameObject > SetActive (here we’re just making the door disappear, but if you had a door with an actual Open() method, that’s where you’d bind it too)
- at the bottom of its inspector, set RequiresKey to true, and in the KeyID field put “MyKey”
Creating the key
- in your project, create a folder, name it Resources
- in it, right click, Create > More Mountains > Top Down Engine > Inventory Engine Key, name it MyKey
- set its ItemID to MyKey, description to “a key”, icon to Koala_Items_MetalKey, TargetInventoryName to SuitMainInventory
- in the Loft3D scene, create a new sphere, name it “MyKeyPicker”, set its collider to IsTrigger, position it at -9,1,0
- add a ItemPicker component to it, drag the MyKey asset we’ve created into its Item slot, add a PickableItem, check DisableObjectWhenDepleted
- press play, DO NOT grab the sphere/key, and walk on top of the small cube next to our “door” (our DoorZone object), press space, nothing happens, as we don’t have the required key
- move right, walk on top of the sphere/key to grab it, press I to open your inventory, notice it’s now in there
- walk on top of the small cube next to the “door”, press space, notice the Door disappears as expected
Creating a chest
Creating a chest is just as simple as creating a door. Create a Key Operated Zone just like before, and then drop a sprite or 3D model into your scene for your chest. On that chest object, add an InventoryEngineChest component. Then all you have to do is add ItemPicker components to it (one per item you want in your chest). Then bind your Chest to your KeyOperatedZone’s inspector just like before, using the InventoryEngineChest.OpenChest method this time. You’re done! When activated, the InventoryEngineChest component will automatically pick all the ItemPickers you’ve added to it and put their items into your character’s inventory. Additionnally, you can add an animator to it to trigger an open animation. Your animator will then need a trigger Animation Parameter called “OpenChest”.
You can find a working Chest example in the KoalaDungeon demo scene.