C#/ Unity:NPC跳过对话线

问题描述 投票:0回答:1

我正在尝试使用对话管理器在我的游戏中处理NPC语音,我遇到了一些麻烦。

我有两个NPC;如果我只与一个人交谈,那么它将通过他们的所有行,没有问题(每个NPC有五行代码,由PC启动并按空格键触发)。但是,如果我和NPC1,NPC2通话,它只会显示NPC2的语音,即使我回到NPC1。

NPC.cs

public class NPC : Character {
    private bool charInRange;
    public Dialogue dialogue;
    public bool talkedTo = false;

    // Use this for initialization
    void Start () {
        charInRange = false;
    }

    // Update is called once per frame
    void Update () {
        //if player is in range and presses space, triggers NPC dialogue
        if (charInRange && Input.GetKeyDown(KeyCode.Space))
        {
            TriggerDialogue();
        }
    }

    //if Player gameObject is in NPC collider, player is in range
    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.tag == "Player")
        {
            charInRange = true;
        }
    }

    //if player exits NPC collider, player is not in range
    void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            charInRange = false;
        }
    }

    //if NPC has been talked to before, displays next sentence; if not, loads dialogue and displays first sentence
    private void TriggerDialogue()
    {
        if (!talkedTo)
        {
            talkedTo = true;
            FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
        }
        else
        {
            FindObjectOfType<DialogueManager>().DisplayNextSentence();
        }
    }
}

DialogueManager.cs

public class DialogueManager : MonoBehaviour {

private Queue<string> sentences;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

//loads a queue with lines from Dialogue and displays first sentence
public void StartDialogue(Dialogue dialogue)
{
    sentences = new Queue<string>();

    foreach (string sentence in dialogue.sentences)
    {
        sentences.Enqueue(sentence);
    }

    DisplayNextSentence();
}

//displays next sentence in the queue
public void DisplayNextSentence()
{
    Debug.Log(sentences.Count);

    //if no more sentences in the queue, end the dialogue
    if(sentences.Count == 0)
    {
        EndDialogue();
        return;
    }

    string sentence = sentences.Dequeue();
    Debug.Log(sentence);
}

//ends dialogue
private void EndDialogue()
{
    Debug.Log("CONVERSATION OVER");
}
}

我知道问题是什么:一旦与NPC2交谈,在NPC.cs翻转谈话,NPC2的对话开始,NPC1的对话队列被消灭。然后,它只显示NPC2的下一句,无论玩家在哪个对手。我的问题是,我不知道如何解决这个问题。我应该在特定NPC中存储特定对话吗?如果是这样,我该怎么做?

任何和所有的帮助表示赞赏。谢谢!

c# unity3d
1个回答
0
投票

@Eddge在评论中回答:

是的,您可以在每个NPC上存储队列,只需让对话系统在与NP通话时显示队列中的下一条消息,而不是让NPC在Dialogue系统上调用一个开始,并让对话系统存储其队列全国人大发出的对话

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.