Making a Treasure Chest System Using Timeline in Unity
Objective: To create a Timeline animation of a Treasure Chest opening with a weapon coming out.
Let’s continue putting the pieces together of everything we have learnt with Timeline in Unity so far. This time, we are going to create a treasure chest opening animation sequence with a weapon coming out of it and a particle effect, ready for the player to collect. This treasure chest opening will also be triggered by the player as they approach it. Let’s get into it!
Similar to other simple scenes I have setup let’s create a basic plane along with the ‘Starter Assets — Third Person Controller’ so we have a character and movement script ready to go. If you are unsure how to import this and get it running, refer to this article.
Treasure Chest Behaviour
For this behaviour, I want the treasure chest lid to rotate open and closed. As the chest is opening I want the weapon and particle effect to play, with the lid closing when they come out. There is also an audio visual cue of the chest opening which you can’t hear.
I have imported the treasure chest from Filebase. It has 2 separate game objects, one for the lid and the other for the bottom half, making the rotation of the top lid nice and easy to setup.
Create a new empty game object, reset it’s transform and name it. Now drag the treasure chest, weapon and particle effect game objects onto this as children.
You could have your weapon hiding somewhere in the chest, or alternatively, instantiate the game object from within the chest:
And your particle effect:
We are now going to set both of these to play/activate on the Timeline.
Create a new Timeline on the game object you created:
Firstly, I have an Audio Clip setup and ready to play as the chest is opened.
Second, this is an Animation Track of the treasure chest lid rotating open and closing, which has been converted to an Animation Clip.
Third, is also an Animation Track of the weapon (staff) that is moving and rotating out of the chest as it is opened (also converted to an Animation Clip).
Lastly, is the Activation Track for the particle effect, which will turn on around the 3 second mark and deactivate automatically when it finishes playing. Make sure to set your particle effect to ‘Play on Awake’ for this. And also, disable this game object in the Hierarchy otherwise it will play when the game starts, we want it to be activated through the Timeline sequence.
Treasure Chest Lid and Weapon Movement/Rotation Keyframes
For the 2 Animation Tracks I have created, they were done from scratch using keyframe position/rotations:
I created a set of start point keyframes for the position/rotation and of course the end point position and rotation keyframes near the 5 second mark. After this I have set the Animation Clip’s Post Extrapolation to ‘Ping Pong’, so that once it finishes it will then play backwards to close it.
For the weapons Post Extrapolation I set it to ‘Hold’ as I am happy for it to stay there.
Now that we have the scene setup, let’s create our simple trigger which is going to happen when the player walks into a Box Collider setup around the treasure chest:
Then we are going to call our PlayableDirector.Play() method from the script that is attached to the treasure chest, which will trigger the timeline to start.
The player walks into the trigger, setting off the timeline sequence.
**Edit** 28–02–2022: Fixed article link in second paragraph.