Home > Enterprise >  Unity OnCollisionEnter2D with multiple gameObjects
Unity OnCollisionEnter2D with multiple gameObjects

Time:08-24

Currently I am creating a brick breaker style game. In this there are two types of bricks. One is normal brick and another is a special brick. Normal brick gets destroyed on collision with ball. Special brick duplicates the ball and then gets destroyed.

Now I have a check that whenever the remaining bricks gets to 0, game is over. I get the number of bricks at start and decrement the bricks on collision with ball.

Issue is that sometimes, the remaining bricks gets to 0 even though I can see that there are few bricks in view. I thought it was due to 2 balls colliding the brick/special brick at same time. So I added hasCollided check and resetting in LateUpdate() to avoid that but issue still seems to happen.

My code snippet:

Ball.cs

void LateUpdate()
{
    this.hasCollided = false;
    this.hasCollidedBrick = false;
}
private void OnCollisionEnter2D(Collision2D collision)
{
    if(collision.gameObject.tag == "Brick")
    {
        if(this.hasCollidedBrick == true){ return; }
        this.hasCollidedBrick = true;
        GameManager.instance.ScoreUp(5);
        Destroy(collision.gameObject);
    }
    else if(collision.gameObject.tag == "SpecialBrick")
    {
        if(this.hasCollided == true){ return; }
        this.hasCollided = true;
        SceneGenerator.instance.remBalls  ;
        GameManager.instance.ScoreUp(5);
        Destroy(collision.gameObject);
        clone = Instantiate(gameObject, transform.position,transform.rotation);
        

    }

}

GameManager.cs

public void ScoreUp(int val)
{
    score=score val;
    if (val == 5)
    {
        remBricks--;
    }
    textScore.text = score.ToString();
    if (remBricks == 0)
    {
        winText.SetActive(true);
        textScore.gameObject.SetActive(false);
        isWin = true;
    }
}

Also, I dynamically generate the bricks at start using Instantiate.

Any help would be appreciated. Thanks!

CodePudding user response:

The only reason for the game finishing faster than intended is ScoreUp() method being called more times than there are bricks, which brings us to when it's called.

I thought it was due to 2 balls colliding the brick/special brick at same time.

Indeed, that seems to be the reason. Your check doesn't work, because the flag is set for both balls separately - both balls will pass the if statement and both will call ScoreUp.

As mentioned in the comments move the flag to the brick or ensure in any other way, that the method will be called only once.

I would also advise against removing (or rather decrementing) bricks based on the score:

score=score val;
if (val == 5)
{
    remBricks--;
}

especially with magic 5 in the if statement, as it's error prone, and may cause more issues with your code later.

  • Related