how start timeline on trigger enter

how i can start timeline on trigger enter for on game cinematic

2 Likes

I took a screenshot from a presentation. Here is an example (see image).

6 Likes

thanks thats really helpful

1 Like

Here’s the simple example script in text form so you don’t have to type it in :stuck_out_tongue_winking_eye:

using UnityEngine;
using UnityEngine.Playables;

public class GenericTrigger : MonoBehaviour
{
    public PlayableDirector timeline;

    // Use this for initialization
    void Start()
    {
        timeline = GetComponent<PlayableDirector>();
    }


    void OnTriggerExit(Collider c)
    {
        if (c.gameObject.tag == "Player")
        {
            timeline.Stop();
        }
    }

    void OnTriggerEnter(Collider c)
    {
        if (c.gameObject.tag == "Player")
        {
            timeline.Play();
        }
    }
}
11 Likes

Recognized your own presentation script? :slight_smile:

what do I need to change to get this working in 2d? the collider part?
right now nothing happens :frowning:

Im using Platformer Pro

any help would make you a god to me

Just change Collider to Collider2D in 15 & 23 line.

1 Like

Thanks Jacob but
OntriggerTimeline.cs(15,31): error CS1001: Unexpected symbol `)', expecting identifier

strangley there is only 30 lines of code not 31…

using UnityEngine;
using UnityEngine.Playables;

public class OntriggerTimeline : MonoBehaviour
{
public PlayableDirector timeline;

// Use this for initialization
void Start()
{
timeline = GetComponent();
}

void OnTriggerExit (Collider2D)
{
if (c.gameObject.tag == “Player”)
{
timeline.Stop();
}
}

void OnTriggerEnter (Collider2D)
{
if (c.gameObject.tag == “Player”)
{
timeline.Play();
}
}
}

using UnityEngine;
using UnityEngine.Playables;

public class OntriggerTimeline : MonoBehaviour {
    public PlayableDirector timeline;

    // Use this for initialization
    void Start()
    {
        timeline = GetComponent<PlayableDirector>();
    }


    void OnTriggerExit (Collider2D c)
    {
        if (c.gameObject.tag == "Player")
        {
            timeline.Stop();
        }
    }

    void OnTriggerEnter (Collider2D c)
    {
        if (c.gameObject.tag == "Player")
        {
            timeline.Play();
        }
    }
}
1 Like

Thank Jacob! the code compiles now!!

I put a box collider 2d and checked trigger…any idea why the guy just stops walking against trigger ? like he cant go in…i thought check marking trigger would change that behaviour :frowning:

If we get this working it will be known as the jacob effect!:roll_eyes:

If you checked the trigger option and still can’t walk into it, then there’s likely another collider component on the GameObject, or on a child of the GameObject.

eff all that…use a prox trigger!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;


public class MotorStart : MonoBehaviour
{
    public GameObject Motor;

    private void OnTriggerEnter(Collider other)
    {
        PlayableDirector pd = Motor.GetComponent<PlayableDirector>();
        if (pd != null)
        {
            pd.Play();
        }
    }
    private void OnTriggerExit(Collider other)
    {
       PlayableDirector pd = Motor.GetComponent<PlayableDirector>();
       
        {
                  
            if (pd != null)
            {
               
             pd.Stop();
             pd.time = 0f;
             pd.initialTime = 0f;
             pd.Evaluate();
             }
        }

        }
}

I was having a ton of issues getting this to do the exit part and return to the start of the playable-- thankfully -splicing works eventually :slight_smile: now this works as a simple on enter on exit to run directors!- well at the most basic level

2 Likes

thanks for your help

I`ve used the script shown by mikew unity, but the timelline starts before the player reaches it.

This is months old but in case anyone comes across this: you probably forgot to uncheck Play On Awake on the Playable Director.

1 Like

Damn Xenophobic. That if statement really did the trick! For some reason the original answer worked, but kept setting my PlayableDirector to “None”.