How to create a Multiple-Response Dialogue System | Unity 2020 Tutorial

How to create a Multiple-Response Dialogue System | Unity 2020 Tutorial

Creating a Dialogue System for NPCs in Unity

In this tutorial, the speaker explains how to create a multiple response dialogue system for NPCs in Unity. The system is versatile and can be modified to fit any game.

Creating an NPC

  • To create an NPC, we need to make a scriptable object that defines what variables we need our NPC to have.
  • We can easily customize each NPC by reusing the same system several times throughout the game.
  • To start, we create a capsule and rename it "NPC Jeff". Then, we give it a material using the Furnace shader.

Making a Dialogue System

  • The trick to making a dialogue system is creating one that can be used over and over again.
  • We want to add dialogue options for our NPC Jeff. Instead of creating new scripts per NPC, which would take too much time and not be efficient, we will use a scriptable object.
  • Scriptable objects are useful tools that allow us to define variables for our NPCs. We define what kind of variables we're going to store in each file.

Implementing Dialogue Options

  • In our public class npc, we define what exactly we need our npc to have. We define variables such as name, greeting message, and responses.
  • We then create another script called "DialogueTrigger" that will handle when the player interacts with an NPC. This script will reference the scriptable object created earlier.
  • Finally, we create another script called "DialogueManager" that handles displaying the dialogue options on screen and allowing players to choose their response.

Conclusion

  • By using scriptable objects, we can easily create and customize dialogue options for NPCs in our game. This system is versatile and can be modified to fit any game.

Setting up Scriptable Object

In this section, the speaker explains how to set up a scriptable object in Unity.

Creating Public Strings for Dialog

  • Create a new public string array called "dialog" to give more room to work with inside the editor.
  • Create another public string array called "player dialog," which will be one less than the NPC's dialogue.

Filling Out NPC Dialog

In this section, the speaker explains how to fill out the NPC dialog and player responses.

Setting Up Parameters

  • Set up parameters for name, dialog size, and player dialog size.
  • Remember that player dialog should always be one less than the NPC's dialogue.

Writing Dialog

  • Write out greeting message for NPC and possible player responses.
  • Respond to each element of the NPC dialog array with an element from the player array (remembering that it is always one off).

Setting Up UI Elements

In this section, the speaker explains how to set up UI elements in Unity.

Creating UI Canvas and Dialog Menu

  • Create a UI canvas in project scene hierarchy.
  • Create an empty object called "dialog menu" as a child of canvas.

Adding Panel and Text Objects

  • Add panel object as child of "dialog menu."
  • Add text object as child of "dialog menu" and rename it "npc name."
  • Duplicate text object and rename it "dialog box."

Adjusting Size and Color

  • Adjust size and color of panel object.
  • Adjust size, position, color, and font size of both text objects.

Creating UI and Script for Dialog Manager

In this section, the speaker explains how to create a UI and script for the dialog manager.

Creating Text Objects

  • To create a new text object, go to the dialog menu and select "Create UI."
  • Duplicate the NPC name text object and rename it "Player Dialog Text."
  • Move the Player Dialog Text below the menu.
  • Change the size of Player Dialog Text to 20.
  • Change the text of Player Dialog Text to indicate that it is where player dialogue will appear.

Creating a C# Script

  • Create a new C# script called "Dialog Manager."
  • Remove the update function from the script.
  • Define several variables in the script:
  • npc (NPC object)
  • isTalking (boolean)
  • distance (float)
  • player (game object)
  • dialogueUI (game object)
  • npcNameText (text object)
  • npcDialogBoxText (text object)
  • playerResponseText (text object)

Defining Variables

  • Define public variables for npc, player, and dialogueUI.
  • Define private variables for isTalking, distance, and currentResponseTracker.
  • Define public variables for npcNameText, npcDialogBoxText, and playerResponseText.

Start Method

  • Create a void start method in which you set dialogueUI.SetActive(false).

On Mouse Over Method

  • Create a void on mouse over method that triggers when your mouse goes over an NPC.
  • Use an if statement to check if you are close enough to interact with an NPC using distance = Vector3.Distance(player.transform.position).
  • Use another if statement to check if you press E key down while not already talking with an NPC. If so, trigger start conversation method.

Creating the Start Conversation Method

In this section, the speaker explains how to create the start conversation method.

Start Conversation Method

  • Create a void start conversation method.
  • Set isTalking to true.
  • Activate dialogueUI.
  • Set npcNameText and npcDialogBoxText text objects to display NPC name and dialog respectively.
  • Set playerResponseText text object to display player response options.

End Conversation Method

  • Create a void end conversation method.
  • Set isTalking to false.
  • Deactivate dialogueUI.

Update Method

  • Create an update method that checks if you press E key down while already talking with an NPC. If so, trigger end conversation method.

Creating the On Mouse Down Method

In this section, the speaker explains how to create the on mouse down method.

On Mouse Down Method

  • Create a void on mouse down method that triggers when you click on an NPC.
  • Use an if statement to check if you are close enough to interact with an NPC using distance = Vector3.Distance(player.transform.position).
  • Use another if statement to check if you press left mouse button down while not already talking with an NPC. If so, trigger start conversation method.

Creating the End Conversation Method

In this section, the speaker explains how to create the end conversation method.

End Conversation Method

  • Create a void end conversation method.
  • Set isTalking to false.
  • Deactivate dialogueUI.

Setting up the Conversation

In this section, the speaker sets up the conversation by resetting the tracker and activating the dialog UI. They also set the NPC name and dialog box text.

Setting Up Conversation

  • Reset tracker to 0 using tracker = 0.
  • Activate dialog UI using dialogUI.SetActive(true).
  • Set NPC name using npcName.text = npc.name.
  • Set dialog box text to greeting message using dialogBox.text = npc.dialog.

Ending the Conversation

In this section, the speaker ends the conversation by deactivating the dialog UI and setting talking to false.

Ending Conversation

  • Set talking to false using talking = false.
  • Deactivate dialog UI using dialogUI.SetActive(false).

Testing in Unity

In this section, the speaker tests their script in Unity by adding a Dialog Manager component and setting up various game objects. They also demonstrate how to trigger conversations with NPCs.

Testing in Unity

  • Add Dialog Manager component.
  • Set NPC archive.
  • Drag NPC into scene.
  • Set up game objects: dialog UI, player name text, NPC dialog text, player response text.
  • Press play to test script.
  • Trigger conversation with NPC by approaching them and pressing "E".

Creating Player Responses

In this section, the speaker demonstrates how to create player responses using mouse scrolling input. They also show how to activate player response text based on current response tracker value.

Creating Player Responses

  • Create new if statement for mouse scroll wheel input: if (Input.GetAxis("Mouse Scroll Wheel") < 0f).
  • Increment current response tracker: currentResponseTracker++.
  • Create new if statement to handle end of conversation: if (currentResponseTracker >= npc.playerDialog.Length - 1).
  • Set current response tracker to last index: currentResponseTracker = npc.playerDialog.Length - 1.
  • Create new else if statement for mouse scroll wheel input: else if (Input.GetAxis("Mouse Scroll Wheel") > 0f).
  • Set current response tracker to previous index: currentResponseTracker--.
  • Create new if statement to handle beginning of conversation: if (currentResponseTracker < 0).
  • Set current response tracker back to zero.
  • Create new if statement for player response text activation: if (currentResponseTracker == 0 && npc.playerDialog.Length >= 0).
  • Activate player response text and set it equal to first element in player dialog array using playerResponse.text = npc.playerDialog.

Creating a Multiple Player Response Dialogue System for Unity

In this section, the speaker explains how to create a dialogue system that allows for multiple player responses in Unity.

Creating Multiple Player Responses

  • Use if statements to activate different player responses.
  • Create new chunks of else if statements for each new player response.
  • Update the numbers in each chunk of else if statements to correspond with the correct player response number.

Adding NPCs and Dialogues

  • Duplicate an existing NPC and rename it.
  • Create a new NPC archive in the project tab.
  • Set up the NPC's name and greeting message, as well as their player responses.

Testing the Dialogue System

  • Select the player character and change the NPC's name to match the newly created one.
  • Play the scene to test out the dialogue system with multiple NPCs and different player responses.

Conclusion

  • All scripts used in this tutorial are available on the speaker's website, along with updates on their current RPG title progress.

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Video description

In this video I will show how to create a simple multiple-response dialogue system for your game! This system is great in that it can be easily customized, and can be modified to work with quests, audio, and even become a book-reading system as seen in Skyrim! As mentioned in the video, the scripts can be downloaded from my website: https://www.mwesterstudios.com/ Also, here is a link to the video by Brackeys explaining more about Scriptable Objects (highly recommended for beginners): https://www.youtube.com/watch?v=aPXvoWVabPY Happy programming!