Triggering a finished cutscene | Unity
A quick guide about how to trigger and return from a finished cutscene in Unity
Objective: Implement a finished cutscene during the gameplay in Unity.
In the last post I covered how to implement security cameras in our game with Unity. Now, it’s time to implement a system to trigger a finished cutscene during the gameplay and returning from it in Unity.
If you’ve been following my recent posts, you’ll remember the cutscene that we composed in the next post:
Well, the cutscene is already finished and the respective animation and activation tracks are synchronized with the Cinemachine cameras:
Our cutscene should be triggered when the player approaches the sleeping guard in the next desk:
Triggering the cutscene
So, in order to trigger the cutscene, let’s create an empty gameobject with a box collider that will detect when the player enters the area:
- Make sure that the collider has the Is Trigger property enabled.
- Attach a new rigidbody and make sure that the Use Gravity property is disabled.
- Create and attach a new script to control the trigger interaction between the empty gameobject with the collider and the player.
Now, let’s open the new script and create a new private variable to store a reference to the cutscene gameobject (which contains the playable director component). Don’t forget to use [SerializeField] to be able to drag the gameobject to the inspector:
Then, to detect the collision between the player’s collider and the empty gameobject, let’s use the OnTriggerEnter method and check if the tag from the other collider belongs to the player using the CompareTag method. If that’s the case, let’s enable the respective cutscene gameobject:
Finally, let’s just drag the gameobject with the playable director that contains our cutscene into the respective slot through the inspector:
If we run the game with Unity we’ll be able to trigger the cutscene when the player enters the area defined with the collider as expected:
But there’s still a problem, when the cutscene ends we remain watching the last frame.
Returning from the cutscene
So, in order to return to the game once the cutscene ends, let’s start by getting rid of the clutter by organizing the respective animation and activation tracks into a tracking group:
Fortunately, there’s an option within the Timeline to implement a solution for our problem: the Timeline signals.
The Timeline signals provide a way to establish communication between the Timeline and external systems. Instead of using a custom script to handle the Timeline when the cutscene ends we can make use of the Timeline signals to react according to our requirements.
So, to start using the Timeline signals, let’s click on the button with a marker to prompt the Timeline markers row. Then, let’s right-click on the row and select to add a new signal emitter. Once added, let’s drag the signal emitter to the last frame of our cutscene (as it will work to disable the cutscene when it ends):
A Timeline signal needs 3 pieces to work:
- A signal emitter
Which we already created in the Timeline and it’s expected to contain a reference to a signal asset.
- A signal asset
Which will associate a signal emitter and a signal receiver and will work as an event within the Timeline.
- A signal receiver
Which will contain a list of reactions that will be linked to a signal asset.
So, let’s click on the signal emitter and create a new signal asset with the next button in the inspector:
Once the signal asset is created, let’s create the respective signal receiver using the button from the inspector. The signal receiver will be created in the gameobject with the playable director of our cutscene.
So, in order to return to the game once the cutscene ends, let’s select the SetActive method to be the reaction of the signal receiver and disable the respective cutscene gameobject when the signal is emitted:
If we select the gameobject that contains our cutscene we’ll be able to see the signal receiver component in the inspector:
If you want to know more about Timeline signals you can visit this Unity blog:
How to use Timeline Signals | Unity Blog
Since Timeline's introduction in 2017, we know that you've been patiently waiting for a way to send events. Well, wait…
And now, if we run the game with Unity and trigger the cutscene we’ll see that when the cutscene ends the signal is emitted and the cutscene gameobject gets disabled and we return to the game:
And that’s it, we triggered and returned from a finished cutscene with Unity! :D. I’ll see you in the next post, where I’ll be showing how to create a manager class with Unity.