unity3d太空射击生命系统碰撞不起作用

时间:2017-04-05 19:52:28

标签: unity3d collision

因为我是Unity3D的新手并且开始使用Space Shooter教程。现在我无法为太空船创造一个简单的生命系统,可能,这是一个愚蠢的错误,但我已经在它上面几个小时,寻找解决方案。

OnTriggerEnter代码为:

void OnTriggerEnter (Collider other)
{
    if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy"))
    {
        return;   
    } 

    if (explosion != null) //hit object explosion
    {
        Instantiate (explosion, transform.position, transform.rotation);
    } 
    if (other.tag == "Player" && playerHealth >= 1) {
        playerHealth--;
        gameController.SubLive (playerHealth);
    }

    if (other.tag == "Player" && playerHealth <= 0) {
        Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
        Destroy (other.gameObject);
        gameController.GameOver ();
    } 
    Destroy (gameObject); //destroy hit object
    gameController.AddScore (scoreValue);
    /**/ 
}

我发现解决方案是每次发生碰撞时减少玩家的健康状况,但是,只有当玩家船只与小行星或敌舰发生碰撞时,它才会发挥作用。触发器是玩家船,敌舰和螺栓(从船上射击)。所有物体都有刚体。你能告诉我我做错了什么吗? 提前谢谢!

您可以找到未经编辑的Space Shooter脚本Here

1 个答案:

答案 0 :(得分:0)

唉!在休息一下并以新鲜的心态回来后,我自己设法解决了! (不过我想的更容易!)

f(x, y)

gameController中的脚本片段:

    void OnTriggerEnter (Collider other)
{
    if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy"))
    {
        return;   
    }

    if (explosion != null) 
    {
        Instantiate (explosion, transform.position, transform.rotation);
    }

    if (other.tag == "Bolt") 
    {
        Destroy (other.gameObject);
    }

    if (other.tag == "Player")
    {
        gameController.SubLive (); //if player ship collides asteroid or enemy ship reduces 1 health
        if (gameController.isDead == true) //explodes ship once playerHealth is 0
        {
            Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
            gameController.GameOver ();
            Destroy (other.gameObject);
        }
    }

    gameController.AddScore (scoreValue);
    Destroy (gameObject);
}