In this article we will cover how to use a trigger to activate a treasure chest opening animation and Cinemachine track.
The trigger game object will allow the treasure chest animation to start playing when the player enters an area.
It is a simple game object with a Box Collider component. Make sure to turn on Is Trigger.
We are going to create a script for this Trigger game object. The first thing is access the Playables library, so we can access the Timeline functions.
We will create a PlayableDirector type variable and serialize it so we can assign the treasure chest director in the the inspector. Using OnTriggerEnter, we can check to see if the tag of the object entering the box collider is the Player. If it is, we can play the Timeline attached to the treasure chest director.
Don’t forget to assign the director in the inspector.
Treasure Chest Director
Create an empty game object and create a Timeline for it. This will be the director for the Treasure Chest sequence.
The trigger will start the sequence, so we want to make sure to turn off Play on Awake.
The first components of the treasure chest sequence will be the chest opening animation, the sword moving up and spinning animation and the activation of the chest glow.
There is going to be a Track and Cart Cinemachine setup.
The Track will go around the chest and end up behind it.
A Virtual Camera is needed to be placed in the Cart when it moves along the Track. It will focus Look At the sword coming out of the chest and Follow the Dolly Cart.
These 3 objects (Track, Cart, Camera) will be disabled when the game starts.
You can add a Cinemachine Track to the Timeline that will allow you to move between different cameras.
The Virtual Camera will be added to this track but you need to assign the camera that has the Cinemachine Brain, which is the Main Camera.
Now the Track, Cart, and Virtual Camera will be added to Activation tracks. The reason for this is if the Track, Cart or Virtual Camera is active it will automatically set the camera in motion. But, we only want it to start to move when the Timeline is played.
The last thing components are an animation for the Player, a particle effect and a sound clip.
These things are position on the Timeline to occur at the same time, so when the player celebrates, the particles play along with a power up sound.
You can see as the Player enter the Box Collider the Timeline is started.
Since we added the Cinemachine Track to the Timeline, when the Track is finished it nicely transition back to the default camera.