We’ve previously been triggering voiceover clips when the player enters specific areas, but it’s finally time we also trigger some cutscenes using some similar code 😉
Objective: Create a modular script for triggering cutscenes when the player enters an area.
1. Just as we did with our previous triggers, the first step is to create the invisible colliders the player avatar will walk through. In my case, I’ve created some empty GameObjects, placed a BoxCollider component on them, and adjusted and positioned them so that the player will walk through them when we want to trigger the cutscene. Do make sure either these objects or the player has a RigidBody and that the colliders on these objects have ‘isTrigger’ set to true (to prevent the player getting stuck on them).
2. Create and attach a new ‘CutsceneTrigger’ script to these objects. The core functionality for these is that they will set a cutscene GameObject to active when the player enters their radius, and will change the value of a boolean to prevent them playing twice. As such, the core logic we need looks like this, making use of that OnTriggerEnter method to detect the player:
3. To make this script as useful as possible, I’d recommend adding in a few extra bells and whistles to this script. Namely, I think the script should be able to disable the cutscene GameObject once the cutscene has finished playing, and I’d also like for it to disable certain GameObjects for the duration of the cutscene (such as the actual player avatar, not the rigged model which appears in the cutscene) and re-enable them at the end. Since we’re most likely using a PlayableDirector component to handle the cutscene itself, let’s use some of the in-built events which will let us know when it’s finished playing. The finished script looks like this:
The UnityEngine.Playables statement at the top of this script is what gives us access to the PlayableDirector component for coding purposes. We can then use the director’s in-built stopped event to trigger some local methods (in our case, one to (re-)enable certain GameObjects, and one to disable the cutscene GameObject. This event does want to pass a PlayableDirector variable as an argument, so that’s why I’ve included it as an argument for both of these methods (not that we’re actually using it there ourselves).
As for these DisableObjects and EnableObjects, these attempt to iterate through any elements in a serialised list of GameObjects (‘_objects’), null-checks them, and sets them to either active or inactive. Because of the way this is written, we could have no objects in the list or even empty/null entries and not run into any exceptions here.
While this script is a fair bit longer than the original, this should more comprehensively handle the logic needed to start and tidy up after any cutscene we specify.
4. All that’s left for us to do now is to head back into Unity, let it compile, and start dragging in the GameObjects and components needed for it to work. In particular, make sure to drag in the cutscene GameObject you want to activate as well as the PlayableDirector for that cutscene. If you want to temporarily hide your Player GameObject or any other GameObject for that matter, increase the size of the Objects array and drag those objects into those slots:
Assuming this script is on the appropriate GameObject (the one with the collider and ‘isTrigger’ enabled), a collider is on the Player, and a RigidBody is on either of these objects (with gravity disabled), we should be seeing our cutscene activate once when the player enters the trigger 😉
This should support a wide array of cutscenes triggered by OnTriggerEnter, but there may well be cases where we want some extra conditions to these triggers (such as whether the Player has collected a keycard). To support this kind of functionality, I’ll most likely return to this script and make use of an enumerated type (or ‘enum’) to specify which type of cutscene this is, but for now this should hopefully suit our purposes.