Good afternoon guys, so I wanted to make a dialogue system in my game I saw a video of a guy doing it, but he only taught how the NPC speaks, not how more than one NPC speaks.
Here is the code for the Dialog Box:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CaixaDialogo : MonoBehaviour
{
public Sprite profile;
public string[] speechText;
public string actorName;
public LayerMask playerLayer;
public float radious;
bool onRadius;
public bool umaVez = true;
public GameObject inimigoTeste;
private CaixaDialogoControl dc;
private void Start()
{
dc = FindObjectOfType<CaixaDialogoControl>();
}
public void FixedUpdate()
{
//Interact();
}
private void Update()
{
if(/*onRadius*/ inimigoTeste == null && umaVez == true)
{
Invoke("Escrever", 0.5f);
umaVez = false;
}
}
/*public void Interact()
{
Collider2D hit = Physics2D.OverlapCircle(transform.position, radious, playerLayer);
if(hit != null)
{
onRadius = true;
}
else
{
onRadius = false;
}
}*/
private void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(transform.position, radious);
}
void Escrever()
{
dc.Speech(profile, speechText, actorName);
}
}
And here is the code for the Control Dialog Box:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CaixaDialogoControl : MonoBehaviour
{
public GameObject dialogObj;
public Image profile;
public Text speechText;
public Text actorNameText;
public float typingSpeed;
private string[] sentences;
private int index;
public GameObject inimigoTeste;
public bool testeTutorial = true;
public bool umaVez = true;
public GameObject Joystick;
public GameObject ButtomTiro;
public Animator animCaixa;
public Animator anim;
void Update()
{
if(inimigoTeste == null)
{
Joystick.SetActive(false);
ButtomTiro.SetActive(false);
Invoke("ChamarCaixaDialogo", 0.01f);
if(testeTutorial == true && umaVez == true)
{
Invoke("ChamarInstrutor", 4.0f);
umaVez = false;
}
}
}
public void Speech(Sprite p, string[] txt, string actorName)
{
profile.sprite = p;
sentences = txt;
actorNameText.text = actorName;
if(inimigoTeste == null)
{
StartCoroutine(TypeSentence());
}
}
void ChamarCaixaDialogo()
{
animCaixa.SetBool("Parado", false);
animCaixa.SetBool("Ativar", true);
animCaixa.SetBool("Desativar", false);
}
void ChamarInstrutor()
{
anim.SetBool("Parado", false);
anim.SetBool("Ativar", true);
}
IEnumerator TypeSentence()
{
foreach (char letter in sentences[index].ToCharArray())
{
speechText.text += letter;
yield return new WaitForSeconds(typingSpeed);
}
}
public void NextSentence()
{
if(inimigoTeste == null)
{
if(speechText.text == sentences[index])
{
//ainda há textos
if(index < sentences.Length - 1)
{
index++;
speechText.text = "";
StartCoroutine(TypeSentence());
}
else //lido quando acaba os textos
{
speechText.text = "";
index = 0;
animCaixa.SetBool("Parado", false);
animCaixa.SetBool("Ativar", false);
animCaixa.SetBool("Desativar", true);
}
}
}
}
}
Here is the video I saw:
If anyone can help me how to make the Player and NPC speak I would be very grateful, because I’ve been at it for HOURS.
Or if anyone has a simpler idea of how to make a dialogue system please tell me because I don’t know what else I can do ;-;