如何编写程序通过进入和退出触发器来使对象出现和消失?

我需要一个代码来使一个对象在我进入触发器对象时出现并在退出触发器对象时消失。


GameObject GhostApparition;

    // Use this for initialization

    void OnTriggerEnter (Collider other)

    {

        if (other.CompareTag("Player")) ;

        {

            //????

        }

    }

我希望对象在我进入触发对象时出现并在我退出对象时消失。实际的网格也应该消失。


HUWWW
浏览 134回答 4
4回答

温温酱

您可以使用 gameObject.SetActive(true);  gameObject.SetActive(false);您只需在 TriggerEnter 和 TriggerExit 中调用它。我不知道你是否想让你的“ghostapparition”不活动,但它是:GhostApparition.SetActive(true); GhostApparition.SetActive(false);

繁花如伊

….monobehavior:public GameObject object;void Start(){    object.SetActive(false);    object2.SetActive(false);}void OnTriggerEnter(Collider other){    if (other.gameObject.tag == "Player")    {        {           object.SetActive(true);           object2.SetActive(false);        }    }}/// void OnTriggerStay and 'object2' are extras (for if u want a keycode to show an additional text)void OnTriggerStay(Collider other){    if (object.activeInHierarchy && Input.GetKeyUp(KeyCode.E))    {        object.SetActive(false);        object2.SetActive(true);    }}void OnTriggerExit(Collider other){    object.SetActive(false);    object2.SetActive(false);}...

LEATH

要解决这个问题,只需这样做:GameObject GhostApparition;    // Use this for initialization    void OnTriggerEnter (Collider other)    {        if (other.tag == "Player") ;        {             GhostApparition.SetActive(true);        }    }    void OnTriggerExit (Collider other)    {        if (other.tag == "Player") ;        {             GhostApparition.SetActive(false);        }    }但是当你使用包含行为的游戏对象时要注意。如果您停用它,然后再次激活,它将再次运行 void Start 并重置您的变量。有时你不会想要它。所以我建议,在这些时候,您只需停用GameObject MeshRenderer组件即可。

qq_花开花谢_0

您可以激活/停用游戏对象,如下所示:gameObjext.SetActive(true); //or false通过激活/停用所有附加组件也将被启用/禁用,doc。
打开App,查看更多内容
随时随地看视频慕课网APP