Coding an Idle Cutscene in Cinemachine and Timeline Part 2
The First Camera angle and Triggering the Scene via Code
In the previous article, I went over how to code an idle cutscene using a coroutine that is triggered after a few seconds of inactivity. Today we will focus on detailing what that cutscene will look like and how to trigger it via code. The scene in question will serve as a means to highlight the environment and art of the project.
Let’s get to it!
→Note: This a follow-up documentation from this article here.
Table of Contents
· How the Scene Looks
· Set up to Code the Trigger
· Coding the Idle Cutscene Trigger
· The Result
I want to start off by adding a camera that flies in a curve around the player object (spaceship). This particular camera will spotlight the specular highlights produced by the metal texture of the spaceship. The perfect camera for this is the dolly track with cart virtual camera, I’m specifically using this variation of the dolly cam because it allows me to set the speed at which the camera will move, and that’s very important for the cinematic feel.
- First, I add the Dolly Track with Cart via the Cinemachine menu.
2. Now, it’s time to start to shape the actual track. After adding some points and shapping the track, this path represents the curve our virtual camera will follow. I may change this as I go along, but for now, this is good enough to start testing for the best camera angles.
3. By accessing the cart in the inspector, I’ll set my speed to be rather slow for starters. The cart will be moving along this path at the selected speed. This is currently using Update as an Update method for the time being, which handles the movement behavior.
4. It’s time to add our virtual camera; the dolly cart is just a path and movement behavior for it. Let’s do this via the Cinemachine menu. Prior to adding the camera, I had my scene view positioned at the starting point I’d like my camera angle to have; that way, the virtual camera will automatically inherit the position when added.
5. The virtual camera will use the Cart as a Follow target and the player (Spaceship) as a Look At targe.
Before continuing, I adjusted the camera to be close to the player, and I also tweaked the cart path.
How the Scene Looks
Set up to Code the Trigger
We need a way to trigger the scene; we already coded a coroutine to handle the timing. Now all we need is something to handle when the Cinemachine sequences start playing.
- Let’s start by adding an Empty GameObject, this will be the Playable Director, which is the Timeline I’ll be using to control when the sequence plays.
Note: I use an empty game object as a best practice method since this makes it easier to track which objects in the scene are being managed by a Timeline Director.
2. I added the dolly cart, track and virtual cameras as children of the empty object to remember they are being managed by a Director.
3. Now, I add the Director component to the empty game object and make sure to disable “Play On Awake” because we want to control exactly when this triggers.
4. Now, I need a Timeline put it all together; this must be created while the Director is selected.
5. Next, I added my Main Camera as a Cinemachine track; this allows me to directly access the Virtual Camera attached to the dolly cart.
I made sure the virtual camera in the Timeline is stretched long enough for the entire movement /animation to play(the length represents how long the camera will be active).
Alright, now we’re ready to code!
Coding the Idle Cutscene Trigger
- Let’s make the Director accessible in the Inspector can access its Playable Director Component with ease.
2. We’re going to look for the Playable Director component in the Start method, which is also the coroutine is handled.
The way this works is that I have an empty game object with a camera manager script attached to it. This handles anything that has to do with cameras in the project, and now I’ve attached the Director object to it in the Inspector. That slot was created via the SerializeField in step 1.
Note: The first part of the code documentation is here. This is where I created the coroutine.
3. I make sure to stop playing the director if any input is detected from the player.
4. Lastly, the coroutine must start playing the Director and we’re all set.
The Result
The coroutine plays when input is not detected.(Image quality has ben reduced to meet Medium’s upload parameters).
The coroutine stops when input is detected.