项目符号销毁2游戏对象意图一统一2D

时间:2019-04-17 07:50:08

标签: c# unity3d unityscript

如果在附近或相同位置有2个球,我的子弹与一个球相撞,则所有物体将同时被破坏。当子弹与其他物体碰撞时,我会在脚本中进行设置,然后它将被销毁。

我试图用其他方法检查每种球的类型

 if(other.gameObject.tag == "yerrow")
    { 
        if (ballType >= 0 && ballType < 4)
        {

            clone1 = (GameObject)Instantiate(_ball, gameObject.transform.position, Quaternion.identity);
            clone1.gameObject.GetComponent<Balloon_Behave>().ballMove = -4;
            clone1.gameObject.GetComponent<Balloon_Behave>().transform.position = new Vector2(transform.position.x - Random.Range(0, 0.5f), transform.position.y - Random.Range(0,1));

            clone2 = Instantiate(_ball, gameObject.transform.position, Quaternion.identity);

            Destroy(this.gameObject);
        }
        else if (ballType == 4)
        {
            Destroy(this.gameObject);
        }
        Debug.Log("Yerrow COLl");
    }

Yerrow Script摧毁了自己

  private void OnTriggerEnter2D(Collider2D other)
      {  
         if (other.gameObject.tag != "Player")
          {
             Destroy(this.gameObject);
             player.CanFire = true;
          }
      }

编辑说明:每个与撞球碰撞的球都会破坏意图。我想用一颗子弹摧毁一个物体

3 个答案:

答案 0 :(得分:0)

您可以结合使用这两个功能,并使用活动标志来检查项目符号状态。

//Check if the bullet is active, then destroy the ball.
if(other.gameObject.tag == "yerrow" && other.gameObject.activeSelf)
{ 
    if (ballType >= 0 && ballType < 4)
    {
        ...
        Destroy(this.gameObject);
    }
    else if (ballType == 4)
    {
        Destroy(this.gameObject);
    }

    //Destroy and deactive the bullet here
    Destroy(other.gameObject);
    other.gameObject.SetActive(false);
}

答案 1 :(得分:0)

在球上,OnCollisionEnter2D

if(collision.collider.tag=="BulletTag")
{
     Destroy(collision.collider.gameObject);
     Destroy(gameObject);
}

仅将其应用于

答案 2 :(得分:0)

设置布尔值以检查子弹是否击中球,如果子弹与球相撞则将其设为假。

 public Player _player

 void start(){
 _powerUP = GameObject.Find("player").GetComponent<Player>();
 }

 if(other.gameObject.tag == "yerrow" && _player.canBe == true)
    {
        _player.canBe = false; // False just after hit
        if (ballType >= 0 && ballType < 4)
        {
            clone1 = (GameObject) Instantiate(_ball, gameObject.transform.position, Quaternion.identity);
            clone1.gameObject.GetComponent<Balloon_Behave>().ballMove = -4;
            clone1.gameObject.GetComponent<Balloon_Behave>().transform.position = new Vector2(transform.position.x - Random.Range(0, 1f), transform.position.y - Random.Range(0,1));
             Instantiate(_ball, gameObject.transform.position, Quaternion.identity);
            Destroy(this.gameObject);
        }
        else if(ballType == 4)
        {
            Destroy(this.gameObject);
        }  
    }

不要在Bullet On触发器中将此值设为true,在调用“实例化克隆”时将其设为true,然后应该可以找到它。