Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How do I make dialogue trigger?

Discussion in 'General Discussion' started by Dpac93, Jul 28, 2018.

  1. Dpac93

    Dpac93

    Joined:
    Jan 15, 2018
    Posts:
    2
    I am trying to create a state where, when a player walks through a collider, dialogue box appears. it could be string of dialogue box playing one after another or just a single one which can either disappear after few seconds of being active or by pressing say "Enter" Key.
    How would I make such mechanic? Thank you.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,526
    Use a trigger collider. See Colliders as Triggers.

    Use Unity UI. See UI.

    You might want to add a small script to the collider GameObject that detects when the player enters the trigger. Something like:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4. public class TriggerEvent : MonoBehaviour
    5. {
    6.     public UnityEvent onTriggerEnter = new UnityEvent();
    7.     public UnityEvent onTriggerExit = new UnityEvent();
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.CompareTag("Player")) onTriggerEnter.Invoke();
    12.     }
    13.  
    14.     void OnTriggerExit(Collider other)
    15.     {
    16.         if (other.CompareTag("Player")) onTriggerExit.Invoke();
    17.     }
    18. }
    This will add two UnityEvents that you can hook up in the inspector -- for example to activate a dialogue box GameObject.

    You can use a coroutine to hide it after a time or to show a series of boxes in sequence.
     
    ippdev and JamesArndt like this.
  3. halo24052001

    halo24052001

    Joined:
    Jan 28, 2020
    Posts:
    5
    Thanks alot i'm trying it