I created a dialogue system with audio it works perfectly fine unless you start skipping the voice lines so then it all becomes a mess with all of the voice lines overlapping is there a way to make it so when you go to the next audio line the previous one stops playing?
as you can see in the video you can switch the text just fine and it doesn't show the previous one but for the audio, it just transforms into a jumbled mess, is there a way to fix this or to at least make it so people cant go to the next line until they finish hearing the current one?
DialogueManager Script:
public class DialogueManager : MonoBehaviour
{
public Door1Animation doorAnimation;
public Text nameText;
public Text dialogueText;
public Text changeText;
public AudioManager Sound;
public Animator textBoxAnimation;
public Queue<string> sentences;
public Queue<string> voiceLines;
void Start()
{
sentences = new Queue<string>();
voiceLines = new Queue<string>();
}
public void StartDialogue (Dialogue dialogue)
{
textBoxAnimation.SetBool("Open", true);
changeText.text = ("Aperte R para continuar");
nameText.text = dialogue.name;
sentences.Clear();
voiceLines.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
foreach (string voiceLine in dialogue.voiceLines)
{
voiceLines.Enqueue(voiceLine);
}
PlayNextLine();
}
public void DisplayNextSentence()
{
if (sentences.Count == 1)
{
changeText.text = ("Aperte R para fechar");
}
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogueText.text = sentence;
}
public void PlayNextLine()
{
if (voiceLines.Count == 0)
{
return;
}
string voiceLine = voiceLines.Dequeue();
Sound.Play(voiceLine);
}
public void EndDialogue()
{
Debug.Log("End of conversation");
doorAnimation.openDoor = true;
textBoxAnimation.SetBool("Open", false);
}
}
AudioManager Script:
using UnityEngine.Audio;
using System;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public Sound[] sounds;
void Awake()
{
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
private void Start()
{
Play("BackgroundMusic");
}
public void Play (string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
}
}
Just the part of the interaction code so you can go to the next line/audio:
public void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
PersonInteract();
buttonInteract();
}
if (Input.GetKeyDown(KeyCode.R))
{
FindObjectOfType<DialogueManager>().DisplayNextSentence();
FindObjectOfType<DialogueManager>().PlayNextLine();
}
}
Inspector view of the dialogue manager: