簡體   English   中英

子彈摧毀 2 個游戲對象意圖一個統一 2D

[英]Bullet destroy 2 game object intent one unity 2D

當我的子彈與一個球碰撞時,如果附近或同一位置有 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 腳本摧毀他自己

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

編輯注意:每個球與 bulled 碰撞都是破壞意圖之一。 我想用一顆子彈摧毀一個物體

您可以結合這兩個功能,並使用 active 標志來檢查項目符號狀態。

//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);
}

在球上,在OnCollisionEnter2D內部

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

僅將其應用到上。

設置 bool 以檢查球子彈是否擊中球,如果子彈與球碰撞則設為 false。

 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 觸發器中設置此值,在調用 Instantiate Clone 時將其設置為 true,然后它應該可以找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM