Unity C#: Typewriter Text Effect (letter-by-letter) [with comments]

using UnityEngine; using UnityEngine.UI; using System.Collections; public class talkScript : MonoBehaviour {//For NPC, but can be changed to be used anywhere 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(player must have a <talking> variable (boolean) too!), 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 () { //Happens every frame 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 talking = false; //not talking now messageBox.SetActive (false); //disable messageBox GUI sprite }else{ //if there IS/ARE line(s) to speak (in message[] array) messageString = ""; //reset messageString lineAt++; //increase line number charAt = 0; //reset charAt } } } } } void OnMouseDown () { //When mouse is clicked if (Vector3.Distance (transform.position, player.transform.position) < 4 && !talking && player.GetComponent<playerScript>().talking==false){ //if the player is close enough and both objects' talking variables are false lineAt = 0; charAt = 0; //reset these variables player.transform.LookAt(new Vector3(transform.position.x, player.transform.position.y, transform.position.z)); //make the player look at the NPC player.GetComponent<playerScript>().talking = true; //set player's talking variable to true talking = true; //is talking messageBox.SetActive(true); //enable messageBox GUI sprite messageString = ""; //reset 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!

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.