简体   繁体   English

我正在努力让游戏 object 在与玩家碰撞时被摧毁

[英]I am struggling to get a game object to be destroyed when colliding with the player

I have written code to heal the player after colliding with the health potion and then destroy the potion game object making it one time use, however, the gameobject does not get destroyed and the player is not healed.我已经编写了代码来在与健康药水发生碰撞后治愈玩家,然后销毁药水游戏 object 使其一次性使用,但是,游戏对象不会被摧毁,玩家也不会被治愈。 Code is as shown below:代码如下所示:

    private void OnCollisionEnter2D(Collision2D collision)
    {

        if (collision.gameObject.tag == "Player" )
        {
            playerHealthScript.heal();
            Destroy(gameObject);
        }
    }
}

(code below is in a seperate script, used for player health) (下面的代码在一个单独的脚本中,用于播放器健康)

    public void heal()
    {
        currentHealth += healingAmount;
        currentHealth = Mathf.Clamp(currentHealth, 0, 100);

        healthBar.fillAmount = currentHealth / 100f;

    }

Before seek for anything, you should Debug.Log("Collision") in your first function, maybe your code is right but the collision isn't detect.在寻找任何东西之前,你应该在你的第一个 function 中使用 Debug.Log("Collision"),也许你的代码是正确的,但没有检测到碰撞。

If so, yo could consideer check if the syntax of "Player" is the same as the tag, or maybe your "Player" doesn't have any rigidbody2D ( 2D is important. ).如果是这样,您可以考虑检查“Player”的语法是否与标签相同,或者您的“Player”可能没有任何 rigidbody2D(2D 很重要。)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM