Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Dialogue Trigger Help

Hi guys,

I've been trying to make a dialogue system for NPCs for a bit, but I've gotten stuck. I can get the dialogue and continue button to work on playing the game in Unity, but I don't know how to make a trigger for it and attach it to NPCs. The endgoal is to get the NPC to say something after the player approaches it and either presses a button or clicks on the NPC. I tried asking the Unity forum and got no replies. I also tried searching elsewhere and got no answers that match the method I'm using. I'm using the new input system.


My code is as follows:

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

public class Dialogue : MonoBehaviour{

 public TextMeshProUGUI textDisplay;
 public string[] sentences; // holds all sentences
 private int index; //int variable
 public float typingSpeed; 
 public string name;
 public GameObject continueButton;
 public Animator animator;


 void update (){

     if(textDisplay.text == sentences[index]){
         continueButton.SetActive(true);
     }
 }


  // Start is called before the first frame update
 public void StartDialogue(){
      animator.SetBool("IsOpen", true);
    
      StartCoroutine(Type());
 }
 
 public IEnumerator Type(){ //coroutine

     foreach(char letter in sentences[index].ToCharArray()){
         textDisplay.text += letter;
         yield return new WaitForSeconds(typingSpeed);
         //delays code by typing speed
     }
 } 

 public void NextSentence(){


     if (index < sentences.Length - 1){ //if we are at the end of dialogue
         index++; //increments the characters
         textDisplay.text = "";
         StartCoroutine(Type());
     } else {
         textDisplay.text = "";
         continueButton.SetActive(false); //disables cont at end of dialogue
         animator.SetBool("IsOpen", false);
     }
 }

}

Answers

  • Do you have anything for the NPC yet? or a 'DialogueManager'?

    Because that is where you would want to put something like this.

    On the NPC, maybe something like:

    OnPlayerInteract += DialogueManager.GetDialogue(4).StartDialogue();



    Short answer, you're going to need an event system of some kind. Custom, or Unity default.

  • jepp21jepp21 Member

    Yes, I have a dialogue manager and the default event system. The dialogue box appears when as soon as I press play and plays the dialogue. Should I drag the dialogue manager script onto the NPC or just make a script with the code you posted?

  • No, the code I posted is useless, it was just for an example.

    And putting it on the NPC is up to you and how your app is structured.

    Are your NPC also Dialogue Managers? or do they just talk to the Manager?


    Do you already have something for NPC? Like a "PlayerInteractable" object? Because there is where you would want to attach StartDialogue.

  • jepp21jepp21 Member

    I tried making a separate script as a trigger that would reference the dialogue script in the manager:


    public class Dialogue_Trigger : MonoBehaviour

    {


        public MonoBehaviour dialogue;

        public GameObject objectWithDialogue;

        


        public void TriggerDialogue () {

            objectWithDialogue = objectWithDialogue; //the object that will run the dialog script

            objectWithDialogue .GetComponent<Dialogue>().StartDialogue();

        }

      

    }


    and then attaching it to a button on the npc with an onclick event, but still no luck. I'm pretty new at this and have been trying to learn as I go.

  • Just add a boxcollider and check is trigger on your NPC. Then do start dialogue with OnTriggerEnter(Collider other) and check if the other variable has a player component.

    if (other.GetComponent<PLAYERCOMPONENT>()

    {


    }

  • ZicrusZicrus Member

    In an OnTriggerStay function, check if the object it collided with is the player. If it is, check if the player pressed the button. If he did, trigger the dialogue

Sign In or Register to comment.