Course contentsShow
← Unity Game Development
Lesson 414 of 8286. Animation and CinemachineFree lesson

Managing Timeline Playback Through Scripting

Control the PlayableDirector component via C# to start, stop, or pause cinematic sequences based on player interactions.

In this lesson, you will learn how to programmatically control your cinematic sequences by manipulating the PlayableDirector component via C#.

While Unity’s Timeline window provides a visual interface for orchestration, hard-coding transitions or interactions—like starting a cutscene when a player steps on a pressure plate or pausing it during a dialogue menu—requires script-level control.

The PlayableDirector Component

The PlayableDirector is the bridge between your Timeline asset and the scene. It acts as the "player" for your sequence. To manage playback, you must first access this component in your script:

using UnityEngine;
using UnityEngine.Playables;

public class CinematicController : MonoBehaviour
{
    public PlayableDirector timelineDirector;

    void TriggerSequence()
    {
        // Start or resume the sequence
        timelineDirector.Play();
    }

    void PauseSequence()
    {
        // Pause current playback
        timelineDirector.Pause();
    }

    void StopSequence()
    {
        // Stop and reset to the beginning
        timelineDirector.Stop();
    }
}

Controlling State

Beyond simple start/stop commands, you can manually scrub through the sequence or jump to specific points by setting the time property. For example, if you want a player to skip a 5-second intro, you could set timelineDirector.time = 5.0f;.

You can also use the state property to check if a sequence is currently Playing, Paused, or Stopped. This is particularly useful when you need to prevent the player from triggering a new cutscene while one is already active, ensuring your camera transitions remain seamless and your story logic stays intact.

Integrating these methods into your existing triggers (like those you previously built for camera volumes) allows you to turn static animations into dynamic, reactive storytelling experiences. By calling Play() exactly when a player hits a collider or finishes a specific gameplay task, you maintain total control over the narrative flow.

Key Takeaway: By referencing the PlayableDirector in your scripts, you can treat your Timeline sequences as dynamic game objects that respond directly to player interactions and logic.