In this tutorial we’ll add treasure chests to the game.
Item Collectors are used to pick up individual items or access item boxes. We’ll use them to add treasure chests that open when interacted with.
We’ll set up:
- item collection dialogue (inventory settings)
- chest open schematic
- item collectors
Let’s get to it!
Item Collection Dialogue #
Open the Makinom editor and navigate to Inventory > Inventory Settings.
Item Collection #
These settings handle the collection of single items via Item Collector components.
Before Collection #
We’ll play an audio clip.
- Play Sound
Enable this setting. - Audio Clip
Select the Success3 audio clip.
You can find it in Assets/Tutorial Assets/NinjaAdventure/Sounds/Game/.
Collection Dialogue #
We can stick with the default setup, which displays a message with the name of the thing we found. You can optionally also use a choice dialogue to let the player select if it should be collected.
All we need to do is select the UI box that’s used to display the dialogue.
- UI Box
Select Dialogue.
Item Box > Item List Settings #
We don’t use item boxes in this tutorial series, but we can set it up just to have it working.
Like with the single item collection dialogue, we just need to select the UI box and can stick with the rest of the default settings.
- UI Box
Select Blue Center.
Save Changes #
Save the changes before we set up the schematic.
Don’t forget to save your changes by clicking on Save Settings at the bottom of the editor.
Chest Open Schematic #
We’ll simply play an animation on the chest (Machine Object) – the animation’s state is named ChestOpen, and we’ll play it directly without setting any parameters.
Navigate to Schematics.
Play Mecanim Animation #
Add Node > Animation > Mecanim > Play Mecanim Animation
This node is used to play a Mecanim animation and doesn’t require any combatant animation setup to work.
- Object
Select Machine Object. - Play Mode
Select Play. - State Name
Set to: ChestOpen
And that’s it for the schematic – save it, e.g. as ChestOpen.
Adding a Chest #
Let’s first add a chest to the Town scene.
Add the TreasureChest prefab to the scene, you can find it in Assets/Tutorial Assets/Prefabs/.
I’ve added mine in the lower left corner of the town.
The prefab is already set up with everything we need, except the Item Collector.
Item Collector #
Add an Item Collector component to the game object using the component menu.
Usually, an item should only be collected once – this is automatically handled for us by the Scene ID setup of the item collector. This is by default enabled and each new item collector will automatically receive a new, unused scene ID. When the player collects the item, that scene ID is marked as collected (for the current scene) and the item will not be available again when returning to the scene. When copying item collectors (i.e. duplicating them in the same scene), you’ll need to manually get a new scene ID by clicking on the Get New Scene ID button, or reset all scene ID’s in the scene using the Reset All Scene IDs button.
- Use Scene ID
This should be enabled and have the scene ID 0 assigned, since this is the first item collector added to the scene.
Collection Settings #
- Collection Type
Select Single.
We collect a single item and don’t use this as an item box.
We can use the default setup, but let’s take a look at some of the settings.
- Destroy Object
Disable this setting.
We don’t want to destroy the game object after the collection, i.e. the open chest will remain in the scene. - Destroy Collected
Enable this setting.
However, we want the collected chests to be gone when we return to the scene.
This setting will destroy the game object if it was already collected when loading the scene. - Show Dialogue
Enable this setting.
The item collection dialogue will be displayed.
The Control Block Settings handle blocking player controls as well as blocking the move AI for all combatants – this would e.g. prevent enemies hunting the player from getting closer during item collection. They should all be enabled by default, and we keep it that way.
We use the Schematic Settings to play the ChestOpen schematic and animate opening the chest.
- Start Schematic
Select the ChestOpen schematic.
Item Settings #
- Spawn Prefab
Disable this setting.
Since we’re using a chest game object, we don’t need to spawn the item’s prefab (if one is set up).
You can add multiple items to an item collector – in Single collection type, one of the added items will be used randomly. We’ll just change the already added item.
- Type
Select Equipment. - Equipment
Select Leather Armor. - Quantity
Select Value >Value.
Set the value to 1.
You can also use the item’s Chance to determine if the item is available for collection or not. The item can be marked as collected if the chance fails when also enabling Set On Fail.
Start Settings #
The item collector is by default set up as an interaction, i.e. using the Interact start type. You can also use different start types, e.g. on trigger enter (requires a collider with Is Trigger enabled).
If you want, you can also enable Mouse Up As Button in the Interaction stetings to open the chest on mouse click.
And that’s it for the town – save the scene.
More Chests #
Let’s also add a chest to the Forest and Deep Forest scenes.
Forest Scene #
Copy the TreasureChest game object in the Town scene to the clipboard (e.g. CTRL+C).
Open the Forest scene (Assets/Scenes/) and paste the copied chest.
Move it somewhere, e.g. I’ve moved it into the upper left corner of the scene.
We only add 1 chest per scene, so the Scene ID of the Item Collector is no issue for us. If you add another chest to the scene, make sure to update the scene ID (e.g. getting a new one via Get New Scene ID button).
Item Collector > Item Settings > Item 0 #
We change the used item.
- Equipment
Select Cloth Vest.
Save the scene.
Deep Forest Scene #
Open the Deep Forest sceneand paste the copied chest.
Move it somewhere, e.g. I’ve moved it at the end of the scene’s path, below the entrance.
Item Collector > Item Settings > Item 0 #
We change the used item.
- Equipment
Select Elemental Brooch.
Save the scene.
Testing #
Hit play and walk up to a chest.
Interacting with it opens the chest and collects the item, great!
That’s it – we’re done with the 2D RPG Quickstart tutorials. Allow me to say some final words in the next and final part of this tutorial series.