So I created a script that when the player stands in front of an object, a text is displayed on screen. But when the player moves away, i also want the text to disappear. I’d also be happy with the text disappearing after a few seconds. I have tried to go for both ways but I couldn’t figure it out. Could someone help me please?
using UnityEngine;
using System.Collections;
public class karte : MonoBehaviour {
bool map = false;
bool text = false;
public Texture MapDummy;
string Message = "Halte 'O' gedrückt, um die Karte anzuzeigen";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject go = GameObject.FindGameObjectWithTag ("Player");
RaycastHit hit;
Ray landingRay = new Ray (go.transform.position, Vector3.forward);
if (Physics.Raycast (landingRay, out hit, 10)) {
if (hit.collider.tag == "PC") {
text = true;
if (Input.GetKeyDown (KeyCode.O)) {
map = true;
} else if (Input.GetKeyUp (KeyCode.O))
map = false;
}
}
}
void OnGUI () {
if (text == true) {
GUI.Label(new Rect(Screen.width/2,Screen.height/2,300,50),Message);
}
if (map == true) {
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), MapDummy);
}
}
}