Search Unity

Preventing spawn overlap from set spawn positions

Discussion in 'Scripting' started by dwill34, Aug 4, 2020.

  1. dwill34

    dwill34

    Joined:
    May 25, 2017
    Posts:
    9
    Something I've been struggling to find out is how I would spawn a random tile, at a random position (from set positions) without them overlapping. Most of the tutorials I have seen have been for preventing overlap from random points in an area, rather than set positions.
    I essentially have two arrays
    One array is for the different tiles I want to be able to spawn
    The other array is for all of the spawn positions (basically empty game objects at set coordinates) that I want to be able to spawn things at
    My code is this:
    Code (CSharp):
    1.     public void SpawnTiles()
    2.     {
    3.        
    4.         while(currentTileCount < tileLimit)
    5.         {
    6.             //Grab random tile prefab from array to spawn
    7.             int tSpawn = Random.Range(0, spawnables.Length);
    8.             //Grab random spawn position from an array of set spawn positions
    9.             int spawnLoc = Random.Range(0, spawnPos.Length);
    10.             GameObject.Instantiate(spawnables[tSpawn], spawnPos[spawnLoc].transform.position, Quaternion.identity);
    11.             currentTileCount++;
    12.         }
    13.        
    14.        
    15.     }
    I'm unsure of how to prevent two tiles from spawning on top of one another.
    One thought I had is that I could remove that spawn position from the array, disallowing it from being a possible spawn. But I would then need to add that position (and any other positions) I removed back to the array after I call a function that basically deletes all the tiles, and I'm unsure of how I'd do that. (assuming that's the better way to do it, I'm not quite sure)
    If anyone has any ideas, I'd love to hear them since I am fairly new to coding and don't understand all too much
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You could copy the array into a list and then just remove the used one from the list each time.
     
  3. dwill34

    dwill34

    Joined:
    May 25, 2017
    Posts:
    9
    This is what I ended up doing. Since I didn't realize arrays were a bit inflexible.
    To anyone curious, this is what it ended up looking like:
    Code (CSharp):
    1. public void SpawnTiles()
    2.     {
    3.        
    4.         //Grab random tile prefab from array to spawn
    5.         int tSpawn = Random.Range(0, spawnableTiles.Count);
    6.         //Grab random spawn position from an array of set spawn positions
    7.         int spawnLoc = Random.Range(0, spawnLocations.Count);
    8.         GameObject.Instantiate(spawnableTiles[tSpawn], spawnLocations[spawnLoc].transform.position, Quaternion.identity);
    9.         spawnLocations.RemoveAt(spawnLoc);
    10.     }
    11.  
    12.  
    13.     public void RemoveTiles()
    14.     {
    15.         spawnLocations.Clear();
    16.         spawnLocations.AddRange(GameObject.FindGameObjectsWithTag("SpawnPoint"));
    17.     }
    The two public arrays I had listed being:
    public GameObject[] spawnPos;
    public GameObject[] spawnables;
    They were changed to:
    public List<GameObject> spawnLocations;
    public List<GameObject> spawnableTiles;
    The RemoveTiles() function won't get rid of the tiles spawned, it just resets the list of the spawn locations and adds all of them back
    The while loop was removed just for me to test it out. Maybe this isn't the best way of doing it, but this works just fine as it's a pretty small game and the amount of spawn positions isn't very high.