玩家在与标记对象碰撞时消失
Player disappears when it collides with a tagged object
我正在开发一个简单的游戏,其目标是帮助玩家捕捉带有标签的特定对象 "Present"。
处理好模型和动画后,我现在正在处理碰撞和计数 UI。
对于碰撞,在我的播放器控制器上(我使用播放器 Unity Standard Assets 中的 ThirdPersonUserController - 这简化了整个动画过程),我添加了以下函数:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Present")
{
Destroy(gameObject);
count = count - 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count == 0)
{
winText.text = "Thanks to you the Christmas will be memorable!";
}
}
但是,像这样,当玩家击中带有标签"Present"的物体时,即使计数正常,玩家也会消失。
我尝试将 OnCollisionEnter
更改为 OnTriggerEnter
,如下所示:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(gameObject);
count = count - 1;
SetCountText();
}
}
然而,当玩家击中带有标签 "Present" 的对象时,它们不会消失。
我的播放器有一个胶囊碰撞器和一个刚体。
带有标签 "Present" 的对象有一个 Box Collider 和一个 Rigidbody。
任何有关如何让我的播放器留在场景中同时移除其他对象和减少计数的指导都将受到赞赏。
几件事。您正在销毁不正确的游戏对象:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Present")
{
Destroy(gameObject); // this is destroying the current gameobject i.e. the player
count = count - 1;
SetCountText();
}
}
更新为:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(other.gameObject); // this is destroying the other gameobject
count = count - 1;
SetCountText();
}
}
始终使用针对性能进行了优化的 CompareTag()
。
设置 Collider IsTrigger
属性 将使用 OnTriggerEnter
事件而不是 OnCollisionEnter
。
On the first physics update where the collision is detected, the
OnCollisionEnter function is called. During updates where contact is
maintained, OnCollisionStay is called and finally, OnCollisionExit
indicates that contact has been broken. Trigger colliders call the
analogous OnTriggerEnter, OnTriggerStay and OnTriggerExit functions.
见docs
您当前的对象正在使用非触发式碰撞器,因此您应该使用OnCollisionEnter
。您在 OnTriggerEnter
中尝试的 CompareTag
调用更可取。你应该使用它。
此外,您目前正在尝试销毁 ThirdPersonUserController
所附加的游戏对象 (gameObject
)。相反,使用 Destroy(other.gameObject);
:
销毁碰撞对撞机 (other.gameObject
) 的游戏对象
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(other.gameObject);
count = count - 1;
SetCountText();
}
}
我正在开发一个简单的游戏,其目标是帮助玩家捕捉带有标签的特定对象 "Present"。
处理好模型和动画后,我现在正在处理碰撞和计数 UI。
对于碰撞,在我的播放器控制器上(我使用播放器 Unity Standard Assets 中的 ThirdPersonUserController - 这简化了整个动画过程),我添加了以下函数:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Present")
{
Destroy(gameObject);
count = count - 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count == 0)
{
winText.text = "Thanks to you the Christmas will be memorable!";
}
}
但是,像这样,当玩家击中带有标签"Present"的物体时,即使计数正常,玩家也会消失。
我尝试将 OnCollisionEnter
更改为 OnTriggerEnter
,如下所示:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(gameObject);
count = count - 1;
SetCountText();
}
}
然而,当玩家击中带有标签 "Present" 的对象时,它们不会消失。
我的播放器有一个胶囊碰撞器和一个刚体。
带有标签 "Present" 的对象有一个 Box Collider 和一个 Rigidbody。
任何有关如何让我的播放器留在场景中同时移除其他对象和减少计数的指导都将受到赞赏。
几件事。您正在销毁不正确的游戏对象:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Present")
{
Destroy(gameObject); // this is destroying the current gameobject i.e. the player
count = count - 1;
SetCountText();
}
}
更新为:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(other.gameObject); // this is destroying the other gameobject
count = count - 1;
SetCountText();
}
}
始终使用针对性能进行了优化的 CompareTag()
。
设置 Collider IsTrigger
属性 将使用 OnTriggerEnter
事件而不是 OnCollisionEnter
。
On the first physics update where the collision is detected, the OnCollisionEnter function is called. During updates where contact is maintained, OnCollisionStay is called and finally, OnCollisionExit indicates that contact has been broken. Trigger colliders call the analogous OnTriggerEnter, OnTriggerStay and OnTriggerExit functions.
见docs
您当前的对象正在使用非触发式碰撞器,因此您应该使用OnCollisionEnter
。您在 OnTriggerEnter
中尝试的 CompareTag
调用更可取。你应该使用它。
此外,您目前正在尝试销毁 ThirdPersonUserController
所附加的游戏对象 (gameObject
)。相反,使用 Destroy(other.gameObject);
:
other.gameObject
) 的游戏对象
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(other.gameObject);
count = count - 1;
SetCountText();
}
}