Start dialogue on button press

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
ujuj04
Posts: 30
Joined: Sun May 21, 2023 7:34 pm

Start dialogue on button press

Post by ujuj04 »

I want a dialogue to appear on a button press. My player has a companion that is following them all the time. I want to make the player to be able to talk to then whenever and wherever by pressing "J". How do I do that? Thanks in advance. However, I want all the other dialogues to be used with "E" like they are already.
User avatar
Tony Li
Posts: 22131
Joined: Thu Jul 18, 2013 1:27 pm

Re: Start dialogue on button press

Post by Tony Li »

Hi,

You can write a script that listens for the "J" key and starts a conversation. Example (using the built-in input manager):

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;
public class CompanionConversation : MonoBehaviour
{
    [ConversationPopup(true)] public string conversation;
    
    void Update()
    {
        if (!DialogueManager.isConversationActive && Input.GetKeyDown(KeyCode.J))
        {
            DialogueManager.StartConversation(conversation);
        }
    }
}
ujuj04
Posts: 30
Joined: Sun May 21, 2023 7:34 pm

Re: Start dialogue on button press

Post by ujuj04 »

Hey, thanks for the help. However, I also deactivate the player capsule in my dialogue system events. Can I do it in code for this particular conversation?
User avatar
Tony Li
Posts: 22131
Joined: Thu Jul 18, 2013 1:27 pm

Re: Start dialogue on button press

Post by Tony Li »

Yes. If the conversation can't automatically find the player -- see Character GameObject Assignments -- then you can manually pass in the player's transform. For example, change this line:

Code: Select all

DialogueManager.StartConversation(conversation);
to:

Code: Select all

var player = GameObject.FindWithTag("Player");
DialogueManager.StartConversation(conversation, player.transform);
ujuj04
Posts: 30
Joined: Sun May 21, 2023 7:34 pm

Re: Start dialogue on button press

Post by ujuj04 »

Yep, it worked, thanks!
User avatar
Tony Li
Posts: 22131
Joined: Thu Jul 18, 2013 1:27 pm

Re: Start dialogue on button press

Post by Tony Li »

Glad to help!
Post Reply