Unity - 游戏对象在击中预制件时不会被破坏
Unity - GameObject won't destroyed when hit a prefab
在游戏中你控制一个球体(Sphere)和两种掉落的盒子:deathCube和goldCube。当 Sphere 击中 DeathCube 时,Sphere 被摧毁,但它没有被摧毁,我不知道为什么。这些立方体是预制件,它们有一个标签(DeathCube、GoldCube)。
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DeathCube")
{
Destroy (gameObject);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "GoldCube")
{
gold++;
}
}
如果 Sphere 击中 goldCube,您将获得积分,但这也不起作用。
尝试将两个 OnTriggerEnter 合并为一个。
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DeathCube")
{
Destroy (gameObject);
}
if (other.gameObject.tag == "GoldCube")
{
gold++;
}
}
我相信第二个会覆盖第一个,不允许 Destroy()
被调用。我本以为编译器会抛出一个错误,但你似乎没有指出这一点。
如果您没有将刚体附加到碰撞中的至少一个对象(球或立方体),则不会启动触发事件。
来自文档:
Notes: Trigger events are only sent if one of the colliders also has a rigidbody attached
来源:https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
在游戏中你控制一个球体(Sphere)和两种掉落的盒子:deathCube和goldCube。当 Sphere 击中 DeathCube 时,Sphere 被摧毁,但它没有被摧毁,我不知道为什么。这些立方体是预制件,它们有一个标签(DeathCube、GoldCube)。
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DeathCube")
{
Destroy (gameObject);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "GoldCube")
{
gold++;
}
}
如果 Sphere 击中 goldCube,您将获得积分,但这也不起作用。
尝试将两个 OnTriggerEnter 合并为一个。
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DeathCube")
{
Destroy (gameObject);
}
if (other.gameObject.tag == "GoldCube")
{
gold++;
}
}
我相信第二个会覆盖第一个,不允许 Destroy()
被调用。我本以为编译器会抛出一个错误,但你似乎没有指出这一点。
如果您没有将刚体附加到碰撞中的至少一个对象(球或立方体),则不会启动触发事件。
来自文档:
Notes: Trigger events are only sent if one of the colliders also has a rigidbody attached
来源:https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html