Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I play a Playable Director via script?

Discussion in 'Timeline' started by Disreceded, Nov 6, 2023.

  1. Disreceded

    Disreceded

    Joined:
    Nov 5, 2023
    Posts:
    2
    I am new to the unity forum so if they're are any issues with my post I am sorry. This post is self-explanatory. Here is what I have:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5.  
    6. public class CameraCutscene : MonoBehaviour
    7. {
    8.     public AudioSource levelMusic;
    9.     public PlayableAsset cutscene;
    10.     public PlayableDirector director;
    11.     private Player player;
    12.     private bool triggered = false;
    13.     private void OnTriggerEnter2D(Collider2D other) {
    14.         if (other.tag == "Player") {
    15.             if (!triggered){
    16.                 print("triggered!");
    17.                 triggered = true;
    18.                 director.playableAsset = cutscene;
    19.                 player = other.GetComponent<Player>();
    20.                 levelMusic.enabled = false;
    21.                 director.Play(cutscene);
    22.                 director.stopped += (ctx) => {
    23.                     print($"{cutscene.name} stopped");
    24.                     levelMusic.enabled = true;
    25.                 };
    26.             }
    27.  
    28.         }
    29.     }
    30.  
    31.  
    32. }
    33.  
     
  2. Disreceded

    Disreceded

    Joined:
    Nov 5, 2023
    Posts:
    2
    I fixed it! I used my timeline animation used the cinemachine camera but I set my camera to follow my player. so I changed the follow variable in the script and it worked.
     
    Yuchen_Chang likes this.