Make A Treasure Chest System Using Timeline
Objective: Use a trigger to start a Treasure Chest Timeline.
First thing we will need are the Chest and the treasure. I downloaded a Chest and Sword from Filebase.
I used the Starter Assets — Third Person Character Controller from the asset store. Next is create a Empty Object for the Chest Timeline.
Next is to create a Timeline that will host the animation of the chest!
I created the animation for opening the chest.
I added in Audio to get the sound of it opening.
Following this, I created another Timeline for Particles and Treasure Reveal.
On this Timeline, I added four active tracks for the confetti.
Next was to create the Sword animation.
After completing the sword animation, I added in the Audio for Collecting a sword / Treasure.
I created a Control Track on the Chest Opening Timeline. I put the Chest Reveal into the control track.
I created an Empty object with a box collider on it and positioned it in front of the Chest.
I have created a script called Chest_Trigger. In this script I will use OnTriggerEnter and then check to see if it is the Player that entered the Trigger.
To interact with the director, we will need to use the Namespace UnityEngine.Playables;.
Next I need to grab a reference to the director.
All that is left for the script is to call the play method of the PlayableDirector.
Last few things to clean us is to make sure the directors are not play on awake.
Next was to make sure the Reveal Timeline was disabled in the Hierarchy.
After playing around I realized that we could start it over multiple times. To fix this, I created a bool _hasAlreadyOpened. I checked to make sure it was false and if it was Play the Timeline and set the bool to true.
And that is all it is to get a chest opening animation done with timeline. If I wanted to, we could of used OnTriggerStay() and waited for player input to start it but for the example of using triggers to open a chest I just went with OnTriggerEnter(). After finishing everything I thought another good thing to add was a camera that zooms in to the chest, maybe another time.
Thank you for reading and have a wonderful day!