如何阻止两个对象相互重叠生成?

How do I stop two objects from spawning on top of each other?

我正在开发一款类似于 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

当一堵墙在硬币顶部生成时,我希望发生的事情是让硬币得到 "deleted"(在这种情况下,只是从它在墙上的位置移动到它原来的位置重生点在屏幕外,因为它全部在硬币中 "pool") 我们将不胜感激。

这可以使用 Collider 来完成,或者在您的情况下,可能 Collider2D. 我猜你已经在使用碰撞器来检测你的玩家是否撞到硬币或墙壁,所以只需添加逻辑来检测碰撞对象是柱子还是玩家。

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