【发布时间】:2020-04-10 20:29:46
【问题描述】:
我正在尝试根据相机作为对撞机和触发器上的对撞机来激活游戏对象。这是我为此编写的简单脚本。它工作得很好,但默认情况下游戏对象已激活,我必须通过一次进入/退出来停用它们。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class reveal : MonoBehaviour
{
public GameObject Stand;
/// <summary>
/// OnTriggerEnter is called when the Collider other enters the trigger.
/// </summary>
/// <param name="other">The other Collider involved in this collision.</param>
void OnTriggerEnter(Collider other)
{
Stand.SetActive(true);
}
void OnTriggerExit(Collider other)
{
Stand.SetActive(false);
}
}
为了解决这个问题,我修改了脚本以在场景开始之前停用游戏对象,这里的问题是触发器/碰撞器交互不再起作用。我认为我添加的那段代码只是永久停用了对象。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class reveal : MonoBehaviour
{
public GameObject Stand;
// Start is called before the first frame update
void Start()
{
Stand = GameObject.Find("Stand");
Stand.gameObject.SetActive(false);
}
//public GameObject Confrontation;
/// <summary>
/// OnTriggerEnter is called when the Collider other enters the trigger.
/// </summary>
/// <param name="other">The other Collider involved in this collision.</param>
void OnTriggerEnter(Collider other)
{
Stand.SetActive(true);
}
void OnTriggerExit(Collider other)
{
Stand.SetActive(false);
}
}
实现这个的正确方法是什么?有任何想法吗?
【问题讨论】:
-
请注意,如果您将脚本附加到游戏对象,如果您尝试调用
SetActive();,它将不会再次激活 -
我已将脚本附加到一个单独的触发器对象,碰撞器与之交互。
标签: c# unity3d game-development