Coding an Idle Cutscene in Cinemachine and Timeline

Dennisse Pagán Dávila
4 min readDec 27, 2023

An idle cutscene is a scene that triggers if no inputs are registered for a given period of time. Right off the bat, you may notice that one of the most important aspects of this is being able to measure the amount of time spent between inputs, or rather, how long has it been since the last input?

In Unity, we may use Time.time to measure this, and it would be perfectly fine, but I want to experiment with using a coroutine instead.

Objectives:

  • Find a way to measure how long it has been since the last input
  • Start a coroutine after a given amount of time
  • Stop the coroutine if any input is detected
  • Measure time again upon interruption; if the cutscene is terminated, measure the time again so that the idle cutscene can play again.

Let’s get started!

Table of Contents
· Using a Coroutine to Measure Time
· Detecting Input and Setting the Counter Properly
· Testing the Coroutine
· Testing the Input Detection and Coroutine Termination

Using a Coroutine to Measure Time

One of the fundamentals of building a coroutine is that it gives you the ability to wait a certain number of seconds before they execute an action.

Here we have our coroutine, and I’ve decided to wait for 5 seconds. The comment there, which is to play the director, will be formally added once we cover that portion of the process.

Detecting Input and Setting the Counter Properly

Three very important things need to happen whenever input is detected.

  • The director needs to stop playing IF it was active to begin with
  • The coroutine must stop playing; this is done in addition to stopping the director since the deactivation of both is done independently of one another.
  • Starting the coroutine again—this is done as a way to reset the timer.

We can do this in this exact order, and thanks to the order of operations in C#, every action will execute in the correct order.

Here we have our Update function, where we should always detect input. Since we can’t possibly go through every key individually, we simply use anyKey to check for any kind of key input Make sure to check for simple key presses and keys that are being held down.

Testing the Coroutine

Before we move on to creating the actual cinemachine part of this process (the cutscene), let’s test our coroutine by adding a simple line of code to it. The debug log highlighted in yellow will print out this message after 5 seconds.

Note: I reduced the wait time to 2 Seconds so that I could capture this gif example.

Testing the Input Detection and Coroutine Termination

Following the same logic as before, we can test that the coroutine is being stopped and that the input is being detected.

To make sure this is working, two things need to happen:

  • The previous debug log “the cutscene has started” must stop spawning in the console
  • The input detected message must appear in the console

Let me clear those messages (accidental input before demonstration), then Right-click with my mouse button to trigger the input detection.

In the next article, we’ll go over how to create the actual cutscene using Cinemachine and Timeline and how to play it within the coroutine!

--

--

Dennisse Pagán Dávila

Software Engineer that specialize in Game Development. Currently looking for new opportunities. LinkedIn: https://rb.gy/xdu8nu