如何阻止两个对象彼此重叠生成?

时间:2019-04-23 02:08:37

标签: c# unity3d 2d

coins stuck inside the upper wall

我正在开发类似Flappy Bird和Jetpack Joyride的游戏,并且我已经尽可能多地添加硬币来购买新角色。但是,当硬币产生时,它会在墙产生位置的右侧产生更远的距离,这样做是为了尝试阻止硬币在墙后产生。但是现在墙壁在硬币的顶部生成了。我该如何解决?

产生列的代码:

if (!GameController.instance.gamePaused)
{
   timeSinceLastSpawned += Time.deltaTime;
   if (!GameController.instance.gameOver && timeSinceLastSpawned >= spawnRate)
   {
       timeSinceLastSpawned = 0f;
       float spawnYPosition = Random.Range(columnMin, columnMax);
       columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
       currentColumn++;
       if (currentColumn >= columnPoolSize)
           currentColumn = 0;
   } //if
} //if

产生硬币的代码:

if(!GameController.instance.gamePaused)
{
    if (!GameController.instance.gameOver)
    {
        timeSinceLastSpawned += Time.deltaTime;

        if (timeSinceLastSpawned>=spawnRate)
        {
            int randCoin = Random.Range(0, 99);
            int coinRand = coinRarity[randCoin];
            switch(coinRand)
            {
                case 1:
                    timeSinceLastSpawned = 0f;
                    float goldSpawnYPosition = Random.Range(coinMin, coinMax);
                    goldCoins[goldCurrentColumn].transform.position = new Vector2(spawnXPosition, goldSpawnYPosition);
                    goldCurrentColumn++;
                    if (goldCurrentColumn >= goldPoolSize)
                        goldCurrentColumn = 0;
                    break;
                case 2:
                    timeSinceLastSpawned = 0f;
                    float diamondSpawnYPosition = Random.Range(coinMin, coinMax);
                    diamondCoins[diamondCurrentColumn].transform.position = new Vector2(spawnXPosition, diamondSpawnYPosition);
                    diamondCurrentColumn++;
                    if (diamondCurrentColumn >= diamondPoolSize)
                        diamondCurrentColumn = 0;
                    break;
                case 3:
                    timeSinceLastSpawned = 0f;
                    float rubySpawnYPosition = Random.Range(coinMin, coinMax);
                    rubyCoins[rubyCurrentColumn].transform.position = new Vector2(spawnXPosition, rubySpawnYPosition);
                    rubyCurrentColumn++;
                    if (rubyCurrentColumn >= rubyPoolSize)
                        rubyCurrentColumn = 0;
                    break;
                case 4:
                    timeSinceLastSpawned = 0f;
                    float emeraldSpawnYPosition = Random.Range(coinMin, coinMax);
                    emeraldCoins[emeraldCurrentColumn].transform.position = new Vector2(spawnXPosition, emeraldSpawnYPosition);
                    emeraldCurrentColumn++;
                    if (emeraldCurrentColumn >= emeraldPoolSize)
                        emeraldCurrentColumn = 0;
                    break;
            } //switch
        } //if
    } //if
} //if

当我在硬币顶部生成一面墙时,我想发生的事情就是让它被“删除”(在这种情况下,它只是从墙中的位置移到屏幕外的原始生成点)因为它们全都在硬币“池”中) 所有帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这可以使用Collider来完成,也可以使用Collider2D. 我猜想您已经在使用对撞机来检测您的玩家是撞硬币还是撞墙,所以只需添加逻辑来检测碰撞对象是圆柱还是玩家。

void OnTriggerEnter2D(Collider2D col)
{
    if(col.name == "playerObject"){
        //Increase coins?
    }
    this.respawn();
}