0
\$\begingroup\$
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();
        }
    }
}

The idea is to create some kind of cut scene. Once the player (First person, in the screenshot i'm just showing the door on the left where the player will come from and the soldiers ) exit the door on the left and is in a specific distance from the soldier the first soldier the closet one will start moving to the player. Later I will make that it will change to close up view on the soldier with some text conversion. That's the main goal.

But the script the original script by the unity tutorial is using triggers. I'm not sure if I should using this way or using somehow by calculating the distance between the player and the soldier to start the timeline ? And how to do it.

Cut scene

After the soldier is moving/walking to the player at some point I will make it to change to a close up view on the soldier with blurry background something like this:

After start moving

But first how should I approach to the triggering part ?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You could use a SphereCollider component for your trigger. Put it on your player's GameObject and set the radius property to the distance at which the cutscene should start. Because it is a sphere, it will always fire at the closest distance to the soldier.


Another option would be to do it via script:

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

public class GenericTrigger : MonoBehaviour
{
    public PlayableDirector timeline;
    public GameObject[] soldiers;
    public GameObject player;
    public float cutsceneDistance = 5f;

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


    IEnumerator CheckDistanceCoroutine()
    {
        // Check distance forever in each frame
        for(;;)
        {
            // Check distance for each soldier
            for(int i = 0; i < soldiers.Length; i++)
            {
                float distance = Vector3.Distance(soldiers[i].transform.position. player.transform.position);
                if(distance <= cutsceneDistance)
                {
                    timeline.Play();
                    // Maybe also destroy the helper behaviour here if it is not needed anymore
                    break; // Leaves the distance check coroutine
                }
            }

            // Wait for next frame
            yield return null;
        }
    }
}

If you need further info to coroutines, see the documentation:

https://docs.unity3d.com/Manual/Coroutines.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .