using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class talkScript : MonoBehaviour {
public string[] message; //String array containing the message. Each unit of the array is a dialogue. Keep it short.
private string messageString; //Will contain the string being printed
private int lineAt = 0; //At which line (unit) are we at currently, in the message[] array?
private float charAt = 0; //At which character are we at currently, in our messageString?
public bool talking, canTurn; //booleans, if the npc is talking AND if he can turn to face the player
public Text txt; //The GUI text element which is placed in the Canvas in the Unity editor where the message will be displayed
public float talkingSpeed; //The speed at which characters are displayed
public GameObject messageBox, player, touch; //the messageBox GUI element (may be a sprite), the player, and the GUI sprite indicating that the line has been spoken and the player can click to continue
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (talking==true){ //if talking
if (canTurn) transform.LookAt (new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z)); //if it can turn, it'll turn around to look at the player
txt.text = messageString; //sets the GUI text element's text to our messageString
if (charAt < message[lineAt].Length){ //if the line hasn't been completed yet
touch.SetActive (false); //disable the touch (continue) GUI sprite
charAt += talkingSpeed/10; //increase characters displayed at the speed specified
messageString = message[lineAt].Substring(0, Mathf.Clamp (Mathf.RoundToInt(charAt), 0, message[lineAt].Length)); //calculate how many characters will be displayed and assign to messageString
} //else if it isn't talking
else{
touch.SetActive (true); //enable the continue GUI sprite
if (Input.GetMouseButtonDown(0)){ //if clicked, proceed this way:
if (lineAt == message.Length - 1){ //if there is no line left to speak (in the message[] array)
player.GetComponent<playerScript>().talking = false; //sets players talking variable to false (optional, but useful)
talking = false; //not talking now
messageBox.SetActive (false); //disable messageBox GUI sprite
}else{
messageString = "";
lineAt++;
charAt = 0;
}
}
}
}
}
void OnMouseDown () {
if (Vector3.Distance (transform.position, player.transform.position) < 4 && !talking && player.GetComponent<playerScript>().talking==false){
lineAt = 0; charAt = 0;
player.transform.LookAt(new Vector3(transform.position.x, player.transform.position.y, transform.position.z));
player.GetComponent<playerScript>().talking = true;
talking = true;
messageBox.SetActive(true);
messageString = "";
}
}
}
You can use this in Unity to make a typewriter-like talking effect, where letters appear one-by-one at a certain speed. This adds a nice effect to the game.
This is made for NPCs but it can be modded for other uses too.
Feel free to use it!
This is made for NPCs but it can be modded for other uses too.
Feel free to use it!
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.