[英]In Unity2D, GameObject gets instantiated but does not trigger what it is supposed to do when collided with (sometimes)
[英]How Do I Destroy The Collided GameObject When The Pickup Button Is Pressed (X) In Unity
我需要让我的游戏对象(拾取)在玩家进入触发器并按下 x 拾取它时被销毁。
这是我到目前为止所得到的。 这是在需要销毁的皮卡上:
public void Update()
{
if (UIManager.XButtonPressed == true)
{
UIManager.AbleTopickUp = false;
UIManager.XButtonPressed = false;
Destroy(this.gameObject);
Debug.Log("Destroyed the pickup.");
}
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.name == "Player")
{
UIManager.AbleTopickUp = true;
}
}
void OnTriggerExit(Collider collision)
{
if (collision.gameObject.name == "Player")
{
UIManager.AbleTopickUp = true;
}
}
这是在与拾音器触发器碰撞的播放器上:
if (UIManager.AbleTopickUp)
{
if (Input.GetButtonDown("XButton"))
{
UIManager.XButtonPressed = true;
if (currentSlot == 1)
{
Slot1_W = 1;
}
if (currentSlot == 2)
{
Slot2_W = 1;
}
if (currentSlot == 3)
{
Slot3_W = 1;
}
}
}
任何帮助将不胜感激
用OnTriggerEnter
替换OnTriggerStay
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.