how to destroy an item after collision

我的未来我决定 提交于 2019-12-11 04:18:13

问题


I am creating an fps game, I have created the gun, the bullet, and the enemy. Now I want to make my enemy destroyed after the collision with bullet. My enemy is a gameobject named Fire and tagged Enemy and my bullet named "Cube 1(Clone)" and tagged "Cube 1(Clone)". I made a script for that:

#pragma strict

function OnTriggerEnter(theCollision : Collider)
{
    if(theCollision.gameObject.name=="Cube 1") 
    {
        Destroy(gameObject);
        Debug.Log("Dead");
    }
}

But it does not work.


回答1:


You need to check the tag not the name. You could check for name but remember it will have "(Clone)".

function OnTriggerEnter(theCollision : Collider)
{
    if(theCollision.tag == "Cube 1") 
    {
        Destroy(gameObject);
        Debug.Log("Dead");
    }
}

If you are not sure that you tagged correctly, you can simply use both checks in your if statement.

if(theCollision.tag == "Cube 1" || theCollision.gameObject.name == "Cube 1(Clone)") 
    Destroy(gameObject);



回答2:


Well since the bullet is tagged Cube 1(Clone) , I would use

if(theCollision.tag == "Cube 1(Clone)"){...}

And would probably would rename the tag to something meaningful, say bullet.



来源:https://stackoverflow.com/questions/26943975/how-to-destroy-an-item-after-collision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!