Talking Code

  1. I need a code so when my character gets within like 3 or something a bar at the bottom will appear, that says, “To talk to (person’s name) press T” (For this one please make it so in the inspector I can change the name of the character, so I can use the same script multiple times. If you can that is.)

  2. I need another code that when you press “T” it initializes the talk. I don’t care whether I can edit in the inspector or not.

  3. I would like also to make it a conversation, but you don’t actually get a choice of what to say… So it may for example go like this:
    Your Character: Hi
    Person you talk to: What do you want?
    and so on…

  4. If possible can you make it all one code?

I thank anyone who helps. I know my rating is bad, but that’s because I happen to be impatient and post the same question multiple times. I have stop this though, unless I wait like two weeks without a useful answer.

*By the way I don’t care what kinda script you use.

You need to try and make it yourself. And if its too complex for you I recommend starting with simpler things. If you need help with coding, I would recommend buying a book on unity (Will Goldstone has a excellent book that really helped me when I started using unity), or reading the unity tutorials(Start with the 3rd person platformer). Good luck :slight_smile:

Here ya go! (apply it to the character that you want to talk & makes sure you player has the tag “Player” )

using UnityEngine;
using System.Collections;

public class SimpleTalk : MonoBehaviour {
	 
	public bool loop;
	public bool lookAt; 
	//public GUIText text;  
	//public Transform target;
	public string[] conversation;
	public float damping = 3.5f; 
	
	private int convoIndex = 0;
	private string currentTalk = ""; 
	private bool playerIn = false;  
	// Use this for initialization
	//private GUIText pGUI;  
	
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		//LookAtIgnoreHeight(target);
	}
	
//	void OnTriggerEnter(Collider col) {
//	
//		if (col.gameObject.tag == "Player") {
//			
//			//col.gameObject.GetComponent<MouseLook>().enabled = false; 
//			//col.transform.FindChild("Main Camera").GetComponent<MouseLook>().enabled = false; 
//			
//			
//		}
//	}
	
	void OnTriggerStay(Collider col) {
		
		if (lookAt) {
			smoothLookAtIgnoreHeight(col.transform); 
		}
		
		bool pressed = false;
		
		if (col.gameObject.tag == "Player") {
		
			currentTalk = conversation[convoIndex]; 
			
			if (Input.GetKeyDown("t")) {
				pressed = true; 
			} 	
			
			if (Input.GetKeyUp("t")) {
				if (pressed = true) {
					convoIndex++; 
					pressed = false; 
					if (convoIndex >= conversation.Length) {
						if (loop) convoIndex = 0; 
						else convoIndex = conversation.Length - 1; 
					}
				}
			}
		}
	}
	
	void OnTriggerExit(Collider col) {
	
		if (col.gameObject.tag == "Player") {
			
			//col.gameObject.GetComponent<MouseLook>().enabled = true; 
			//col.transform.FindChild("Main Camera").GetComponent<MouseLook>().enabled = true; 
		
			currentTalk = ""; 
			playerIn = false; 
		}
	}
	
	void OnGUI() {
			
			/**if (!pGUI)*/ GUI.Label(new Rect(0,0,Screen.width,20), currentTalk); 
			//else pGUI.text = currentTalk; 
	}
	
	void LookAtIgnoreHeight (Transform target) {

    	Vector3 lookAtPos = target.position;
    	//Set y of LookAt target to be my height.
    	lookAtPos.y = transform.position.y;
  	 	transform.LookAt(lookAtPos);
	}
	
	void smoothLookAtIgnoreHeight(Transform target) {
			Vector3 targetPos = target.position;

            targetPos.y = transform.position.y; 
			
			Quaternion rotation = Quaternion.LookRotation(targetPos - transform.position);
			transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
	}
	
}