Mobile Game Progression
Adding Treasure Chests
What is better than adding some collectable Diamonds scattered around the game? Adding in hidden treasure chest of course! In this article we will focus on creating and adding treasure chest that open up when the player gets close. It will also spawn in additional diamonds and play some special particle effects.
Creating the Treasure Chest:
Since I have the ability draw and animate, I decided to draw my own assets for the game. I drew the chest in its close position before slowly drawing new frames of it opening just slightly until fully opened.
In total there will be 5 frames of animation for the chest opening, and the plan is to have the chest stay open after the player has collected from the chest.
I save and import the images to Unity to have everything sliced and made into individual images. Then, like any beginnings to creating an object with animation, I draw the first image (the image of the chest closed) into the scene and added an Animation component.
Next I added a box collider 2D component and set it to be a Trigger. Finally I created a new empty child object that is going to house the particle system. The plan is to have the particle system to play once the chest is opened.
Scripting the Treasure Box:
We are going to be creating a new C# script called “Treasure Chest”. This will control the function of the treasure chest when it makes contact with the player, along with rewarding with diamonds. It will also control the sound effect and particle system.
Start by giving some handlers to the particle system, reward diamonds, audio Source, Animator and also a bool called “isOpened” and set it to false by default. We are also going to add in some extra handles that are for later usage such as an int count for gems and Transform for chest location, these handlers will control the spawning of the reward diamonds.
In the start function we are first going to assign from of the handlers some variables. Then at the same time we want the particle system to be off since we only want it to play during the opening of the chest.
Next we are going to create an private On Trigger Enter 2D event where to check whenever the object makes a collision with the player tag. And if the player does make contact, the boolean “isOpened” must be in a state of false because the animation and sound effect to play. The reasoning behind this is to stop the sound effect and animation from replaying whenever the player returns and makes contact with the already open chest.
Within the If statement we are going to let everything happen. The particle system is set to play, the animator is set to trigger the animation parameter. The boolean “is Openned” is set to true so the entire chest opening can only occur once. And we are going to have our sound SFX play all while having the reward diamonds get instantiated. You noticed that we have two methods being called that haven’t been setup, and we are going to set them up below.
We have an IEnumerator setup to stop the particle system from continuously playing, it will stop its function after 3 seconds.
Then we have the actual rewarding of the Diamonds which is the same system as the enemies dropping the diamonds. The only difference is that instead of instantiating sideways we are going to instantiate the diamonds vertically.
Back in Unity we are going to drag and drop all the necessary components into the script slots such as the particle system and reward Diamonds. You can also set the amount of gems you want to spawn per treasure chest, so if you fancy a bigger chest that is harder to get, you can reward the players with more diamonds than regular ones.
We are also going to create a new empty child object to house the audio source component, where we can drag in the audio clip to play during the chest opening.
The end result should be something similar to this: