DodgeCoder
DodgeCoder

Reputation: 17

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

coins stuck inside the upper wall

I'm developing a game kinda like Flappy Bird and Jetpack Joyride and I have gotten as far as adding in the coins to purchase new characters with. But, when a coin spawns in, it spawns farther to the right of where a wall spawns and doing this was an attempt to try and stop the coins from spawning behind a wall. But now the walls are spawning on top of the coins. How can I fix this?

Code for the column spawning:

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

Code for the coins spawning:

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

What I'd like to have happen when a wall spawns on top of a coin is have it so that the coin gets "deleted" (in this case just moved from its location in the wall to its original spawn point off screen since its all in a coin "pool") All help will be appreciated.

Upvotes: 0

Views: 336

Answers (1)

Mars
Mars

Reputation: 2572

This can be accomplished using Collider or in your case, probably Collider2D. I'm guessing that you're already using colliders to detect if your player hits the coin or wall, so just add in logic to detect if the colliding object is a column or a player.

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

Upvotes: 1

Related Questions