Hello Humans on the Unity Fourms!
I have a question, so i want to build a game, and i want to have multiple voice options for the player to choose from. but the problem is i want it to play the specific voice at one part of the game, but then if the player wants to change the voice, i dont want it to play the original voice and i want to play the selected voice
Thank u for ur time!
Raine/Luna!
I think you should elaborate your dialogue system in a way that each line has more than once voice-sound. Then when you have to play the line, you could check which index is referred to by your character settings.
Does that make sense for your project?
so lets say if i were someone named toby, i want it to play the voice lines of toby, and when i change the character to someone like angel, i want it to play the voice lines of angel, not toby, im kinda a beginner on using unity.
Raine/Luna
Here’s an overview of how you could manage dialogue code. This compiles, but lacks error checking and a bunch of cool features, but it should get you going:
// A way to identify clearly the voice used
public enum Voice
{
Invalid, // consider implementing a default voice or warning line
Alice,
Bob,
Carol,
Dave,
Eve
}
// A way to store all the variations of a line
// depending on who's saying it
public class DialogueLine
{
public Dictionary<Voice, AudioClip> voices;
}
// A way to bundle all the lines of a conversation together
public class Dialogue
{
public List<DialogueLine> lines;
}
// A way to play the line and do a bunch of verifications
[RequireComponent(typeof(AudioSource))]
public class DialoguePlayer : MonoBehaviour
{
// You should have some kind of placeholder to highlight
// when a line is missing
private AudioClip warningAudioClip;
public bool PlayLine(DialogueLine line, Voice voice)
{
if (line != null)
{
AudioSource audioSource = GetComponent<AudioSource>();
if (line.voices.ContainsKey(voice))
{
audioSource.PlayOneShot(line.voices[voice]);
return true;
}
}
audioSource.PlayOneShot(warningAudioClip);
return false;
}
}
[RequireComponent(typeof(DialoguePlayer))]
public class DialogueExample : MonoBehaviour
{
// All the lines that could be said by everyone
public AudioClip
aliceLine0, bobLine0,
carolLine1, daveLine1, eveLine1,
aliceLine2, bobLine2,
carolLine3, daveLine3, eveLine3;
public void Test()
{
// Build a dialogue
// You should probably store your dialogues as ScriptableObjects
// to save/edit/manage them better
Dialogue dialogue = new Dialogue
{
lines = new List<DialogueLine>
{
new DialogueLine
{
voices =
{
{ Voice.Alice, aliceLine0 },
{ Voice.Bob, bobLine0 },
}
},
new DialogueLine
{
voices =
{
{ Voice.Carol, carolLine1 },
{ Voice.Dave, daveLine1 },
{ Voice.Eve, eveLine1 }
}
},
new DialogueLine
{
voices =
{
{ Voice.Alice, aliceLine2 },
{ Voice.Bob, bobLine2 },
}
},
new DialogueLine
{
voices =
{
{ Voice.Carol, carolLine3 },
{ Voice.Dave, daveLine3 },
{ Voice.Eve, eveLine3 }
}
}
}
};
DialoguePlayer player = GetComponent<DialoguePlayer>();
Voice playerVoice = Voice.Alice; // you could load this from your own PlayerSettings
Voice npcVoice = Voice.Dave;
// Play the dialogue lines with the relevant voices
player.PlayLine(dialogue.lines[0], playerVoice);
// ...later
player.PlayLine(dialogue.lines[1], npcVoice);
// ...later
player.PlayLine(dialogue.lines[2], playerVoice);
// ...later
player.PlayLine(dialogue.lines[3], npcVoice);
}
}