DialogTrigger

Triggering Dialog with Collider2D interactions

This script provides a way to have a Collider2D that is configured as a trigger, so that it can have a Conversation ScriptableObject triggered to be executed by the DialogManager. See version 2 below, it also includes logic to use key-value pair data to determine if a dialog should be replayed when revisiting a scene. The DialogManager version2 has logic that sets conversations as completed by default so they won't be replayed when returning to a scene.

DialogTrigger.cs (Important: see version 2 below)

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

/// <summary>
/// This class provides a logic for the player to trigger dialog using a 2D collider
/// This script will be added to a gameObject with a  collider2D with isTrigger true
///
/// THis script requires that the dialogManager instance which will execute the dialog
/// must be populated in the inspector panel
/// as well as the ConversationList scriptableObject that will be displayed when triggered
///
/// This can be used on multiple objects within a scene, where each instance will have it's own
/// conversation to be exectued. 
/// </summary>
public class DialogTrigger : MonoBehaviour
{
    public ConversationList convList;   //populate in the inspector
    public DialogManager dialogManager; //Populate in the Inspector
   

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            Debug.Log("Collided with DialogTrigger");
            if (dialogManager != null) //populated in the inspector
            {
                dialogManager.TriggeredDialog(convList); //pass the convList as a parameter into the DialogManager
                Debug.Log("Trigger dialog");
            }
            else
            {
                Debug.Log("Couldn't find dialogManager to trigger");
            }
        }

    }
}  //end class

DialogTrigger version2

In this version, a series of values associated with a given key, will determine conditions when the conversation will be replayed in a scene. This requires that the key-value pair: key: ConvList.name value: "completed" be removed from the choiceData dictionary so that the DialogManager will replay the script.

Last updated

Was this helpful?