Home > database >  How to stop Instantiated prefabs from overlapping - Unity 2D?
How to stop Instantiated prefabs from overlapping - Unity 2D?

Time:05-31

As the title says, I am wondering how I can stop prefabs from overlapping?

Here is my current code

{
    List<GameObject> prefabList = new List<GameObject>();
    public GameObject Room1;
    public GameObject Room2;
    public GameObject Room3;


    void Start()
    {
        prefabList.Add(Room1);
        prefabList.Add(Room2);
        prefabList.Add(Room3);

        for (int i = 0; i < 5; i  ) 
        {

            int prefabIndex = UnityEngine.Random.Range(0, prefabList.Count - 0);
            var position = new Vector3(Random.Range(-20.0f, 20.0f), Random.Range(-10.0f, 10.0f));
            Instantiate(prefabList[prefabIndex], position, Quaternion.identity);
        }
    }
}

I want to retry placing the prefab in a different location but do not know how to go about this?

Any help is apreciated.

CodePudding user response:

First you need to find out how big your room is. Once you have that have that data in a variable, lets call it offSet. Once the first room is made you need an if statement to see if there is a room in the position of your next prefab. If there is then offset the 2nd prefab by the distance of the room length or width.

Instantiate(prefabList[prefabIndex], offSet, Quaternion.identity);

CodePudding user response:

There are a few ways to handle this, but it's an interesting problem to have.

With this line of code:

var position = new Vector3(Random.Range(-20.0f, 20.0f), Random.Range(-10.0f, 10.0f));

You are randomly distributing objects throughout the room, but it is possible that you run out of viable space in the room with a higher number of objects spawned.

There are a few ways games solve the problem of "overlapping objects", but I prefer to use the grid system in cases like this rather than trying to involve physics in level generation.

Grid system

You should identify your largest prefab object that you want to spawn, and then break up your map into a grid with cells of that size or larger. Then you can create a List of Tuples that each represent one cell on that grid. Randomly select 1 tuple from the list and remove it when spawning an object to guarantee that no other objects will appear in this area.

    [SerializeField]
    List<GameObject> prefabList = new List<GameObject>();
    [SerializeField]
    int numberToSpawn = 5;
    [SerializeField]
    Vector3 mapAnchor = Vector3.zero;
    [SerializeField]
    float mapWidth = 40f;
    [SerializeField]
    float mapHeight = 20f;
    [SerializeField]
    float cellSize = 1f;
    // Start is called before the first frame update
    void Start()
    {
        //calculate the number of cells along the width and height of the map
        int cellWidth = Mathf.RoundToInt(mapWidth / cellSize);
        int cellHeight = Mathf.RoundToInt(mapHeight / cellSize);
        //Generate a list of tuples that represent each individual cell on the map
        List<(int,int)> cells = gridInstantiate(cellWidth, cellHeight);

        for (int i = 0; i<5; i  )
        {
            
            //Randomly select the cell on the grid
            int randCellIndex = Random.Range(0, cells.Count);
            (int x, int y) = cells[randCellIndex];
            cells.RemoveAt(randCellIndex);
            //Instantiate the object at x and y on the grid, converting it back into world space
            Vector3 position = mapAnchor;
            position.x = position.x   x * cellSize;
            position.y = position.y   y * cellSize;
            Instantiate(prefabList[Random.Range(0, prefabList.Count)], position, Quaternion.identity);
        }
    }

    List<(int,int)> gridInstantiate(int cellWidth, int cellHeight)
    {
        List<(int,int)> cells = new List<(int, int)>();
        for (int i = 0; i < mapWidth; i  )
        {
            for (int j = 0; j < mapHeight; j  )
            {
                cells.Add((i, j));
            }
        }
        return cells;
    }

This is what it looks like generating random circles with the above code:

Example of what it looks like generating 5 circle sprites

To make the objects spawn more naturally, you can add some extra code to smooth that out by giving each cell a buffer, and allowing the instantiation to randomize within that buffer as an offset, preventing them from aligning so perfectly:

Add another property:

    [SerializeField]
    float cellBuffer = 1f;

Modify the cellheight/cellwidth calculation:

        int cellWidth = Mathf.RoundToInt(mapWidth / (cellSize   cellBuffer));
        int cellHeight = Mathf.RoundToInt(mapHeight / (cellSize   cellBuffer));

Give the position a random offset within the limits of the buffer:

            Vector3 offset = new Vector3(Random.Range(-cellBuffer / 2, cellBuffer / 2), Random.Range(-cellBuffer / 2, cellBuffer / 2), 0);
            Instantiate(prefabList[Random.Range(0, prefabList.Count)], position   offset, Quaternion.identity);

This is what the above code changes add, with another isometric diamond sprite set up as another potential prefab and the numberToSpawn set to 30: Example of buffered generation

Why do this, rather than randomly generate and check for overlap?

It's possible to run into a race condition if you randomly generate and check for overlap and there is zero space for a new object. You either don't spawn an object, or keep trying to spawn an object and fail. With this strategy, the list of cells will contain all remaining "open" positions on the map to spawn an object onto, so you don't need to perform any overlap checks. Additionally, when that list is empty you can know that there is no more room to spawn an object.

CodePudding user response:

When using a random number, it is possible for instantiates to overlap. As far as I understand your question, you intend to avoid instant overlapping. For fix this problem you must use the recursive function. This code measures the collider at the chance point and executes the instantiate action if there is no object.

public void TryInstantiate(GameObject prefab)
{
    var position = new Vector3(Random.Range(-20.0f, 20.0f), Random.Range(-10.0f, 10.0f));

    // !Physics2D.OverlapPoint(position) for 2D

    if (!Physics.CheckBox(position, prefab.transform.localScale / 2))
    {
        Instantiate(prefab, position, Quaternion.identity);
    }
    else TryInstantiate(prefab);
}

As you can see below, about 200 cubes were reproduced without any overlap.

for (int i = 0; i < 200; i  )
{
    int prefabIndex = Random.Range(0, prefabList.Count - 0);

    TryInstantiate(prefabList[prefabIndex]);
}

enter image description here

Also make sure your objects have a collider.

  • Related