如何在检测到触发时添加UI文本? 我有这段代码来检测玩家是否在触发器中,但是如果玩家进出触发器,我希望在画布上出现一个消息“Map on”/“Map off”。谢谢!检测到触发时添加UI文本
public class MapDetect : MonoBehaviour {
private bool isTriggered;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
isTriggered = true;
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
isTriggered = false;
}
void Update(){
if(Input.GetKey(KeyCode.Space)){
Debug.Log(isTriggered);
}
}
}
更新代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DetectarMapa : MonoBehaviour {
public GameObject T1;
public GameObject T2;
public float time = 3;
void Start()
{
T1.SetActive (true);
StartCoroutine(Message1());
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
T2.SetActive (true);
StartCoroutine(Message2());
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
T1.SetActive (true);
StartCoroutine(Message1());
}
IEnumerator Message1()
{
yield return new WaitForSeconds(time);
T1.SetActive (false);
}
IEnumerator Message2()
{
yield return new WaitForSeconds(time);
T2.SetActive (false);
}
}
你甚至没有尝试过。你不能实现点击按钮[part](http://stackoverflow.com/q/42632196/3785314)?这是很好的问和得到答案,但也很好,以表明你已经尝试了一些。否则,你希望人们从头到尾编写你的游戏脚本。 – Programmer
对不起!现在,我没有时间来实现点击按钮部分。当我尝试它时,我会写在这里。 – Angelsm