UNITY 2D NPC DIALOGUE SYSTEM TUTORIAL

preview_player
Показать описание
In this tutorial we will look at how to make an NPC system with a dialogue box similar to games like Stardew Valley!

********************************

********************************

If you have any problems, let me know in the comments!

Bye :)
Рекомендации по теме
Комментарии
Автор

For the people struggeling with getting the code to work correctly; I found that the statement (dialogueText.text == dialogue[index]) always returned false until you tried talking to the npc for the second time. This was because the length of dialogueText string started as 1. To fix this you just have to add:

void Start()
{
dialogueText.text = "";
}

And for people wanting to change the continue button out with an interact button like E, you can use this code instead:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class NPC : MonoBehaviour
{
public GameObject dialoguePanel;
public TextMeshProUGUI dialogueText;
public string[] dialogue;
private int index = 0;

public float wordSpeed;
public bool playerIsClose;


void Start()
{
dialogueText.text = "";
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && playerIsClose)
{
if
{

StartCoroutine(Typing());
}
else if (dialogueText.text == dialogue[index])
{
NextLine();
}

}
if (Input.GetKeyDown(KeyCode.Q) &&
{
RemoveText();
}
}

public void RemoveText()
{
dialogueText.text = "";
index = 0;

}

IEnumerator Typing()
{
foreach(char letter in
{
dialogueText.text += letter;
yield return new WaitForSeconds(wordSpeed);
}
}

public void NextLine()
{
if (index < dialogue.Length - 1)
{
index++;
dialogueText.text = "";
StartCoroutine(Typing());
}
else
{
RemoveText();
}
}

private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
playerIsClose = true;
}
}

private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
playerIsClose = false;
RemoveText();
}
}
}

lovfall
Автор

ur tutorials are really cool and helpful, i appreciate ur existence

NoahKDD
Автор

Holy cow! This is beautiful and can scale to multiple NPC's very easily. Outstanding work!

JMacify
Автор

thank you so much for this tutorial!! i was stuck on this for over 10 hours, i just couldn't do it and i've tried multiple tutorials but they didn't work or requiered 3 or more scripts which is confusing to me as a beginner in C#. this tutorial worked and i'm SO glad!!!! thanks!!!

clydehelder
Автор

thank you so much for this tutorial! i was struggling with my dialogue system for a long while until i found this and it works perfectly!!

lucalaxy
Автор

glad to see your still making videos!
your multiplayer serries was very helpful.

Making_dragons
Автор

I don’t know what to say! I’m begging you keep making these wonderful videos please!!

NotEvo
Автор

It's a good tutorial and it works fine on 1 NPC, but I have problem with the other NPC's continue button as it doesn't continue

GamingIndustry
Автор

exactly what i needed! thank you for a simple and easy to follow tutorial!

stevenvictor
Автор

You’re the type of guy to say “you get what you get and you don’t throw a fit”

burnttoast
Автор

this is insanely helpful thank you so much! loads more helpful than so many other tutorials i tried lol

zufoxz
Автор

Thank you very much I'm from Thailand I got a lot of knowledge from you. i made it is a big project Without your script it would be bad. Thank you for giving good things to each other.😍😍🙏🙏

kamonchanok
Автор

ngl, the code just doesn't work at all for me.

ressnezinau
Автор

What a great Tutorial, your channel deserves more subscribers 👍

RadicalTrailers
Автор

Hi! Thanks for the tutorial, but how can i do dialogue with more than one npc??

meowsonya
Автор

I think there is a bug for when you have long sentences and keep pressing on whatever button you have for interacting with the npc, letters from the other sentences start appearing. Any ideas on why this is happening?

DarkkneZPanda
Автор

Yo thank you so so much dude! made it so easy to follow as well!

danieldennison
Автор

Incredibly good tutorial! Thank you :D

lovfall
Автор

Awesome video for people like me who don't know how to program!!

rainilemon
Автор

Thanks for the help, finally have a dialogue system working because of this video, is there a way to disable the E button once a dialogue is active so the player can cycle through all the texts using the continue button and not accidentally closing the dialogue panel by pressing E a second time

adamhyland