【问题标题】:gameObject activates without collision. How to disable gameObject activation on scene start?游戏对象在没有碰撞的情况下激活。如何在场景启动时禁用游戏对象激活?
【发布时间】: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


【解决方案1】:

为了仍然能够使用您的脚本,您可以创建另一个空游戏对象,然后添加该对象的脚本,而不是直接在您要禁用的对象上。

我不确定我是否完全理解你的问题,如果你只是想隐藏游戏对象以便你看不到它但仍然能够与对撞机交互。你可以这样做

 void OnTriggerEnter(Collider other)
    {
        Stand.GetComponent<MeshRenderer>().enabled = true;
    }
    void OnTriggerExit(Collider other)
    {
        Stand.GetComponent<MeshRenderer>().enabled = false;   
}

 }

【讨论】:

  • 感谢您的回复和脚本,CubeCrafter360。我有一个触发器对象,脚本附加到该脚本上,碰撞器与之交互并控制对象 Stand 的激活(如果有意义)。相机/玩家有对撞机。希望这很清楚。
  • 你的建议是 SetActive(true/false) 的替代方案是我的猜测。我的主要问题是,当场景开始时,没有任何交互,游戏对象已经被激活,即使我默认关闭它们。
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多