How do i spawn objects apart from each other?

ı have a little spawner code which spawns little circless randomly on the screen but sometimes circles spawn on top of each other. How can i fix that? i want them to not to spawn on top of each other. Here is my spawner code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    public int numberToSpawn;
    public List<GameObject> spawnPool;
    public GameObject quad;


    void Start()
    {
        spawnObjects();
    }
    public void spawnObjects()
    {
        int randomItem = 0;
        GameObject toSpawn;
        MeshCollider c = quad.GetComponent<MeshCollider>();
        float sccreenX, screenY;
        Vector2 pos;

        for (int i = 0; i < numberToSpawn; i++)
        {
            randomItem = Random.Range(0, spawnPool.Count);
            toSpawn = spawnPool[randomItem];
            sccreenX = Random.Range(c.bounds.min.x, c.bounds.max.x);
            screenY = Random.Range(c.bounds.min.y, c.bounds.max.y);
            pos = new Vector2(sccreenX, screenY);
            Instantiate(toSpawn, pos, toSpawn.transform.rotation);
        }
    }
    private void destroyObject()
    {
        foreach (GameObject o in GameObject.FindGameObjectsWithTag("Food"))
        {
            Destroy(o);
        }
    }
}

You could check the distance of your newest created pos Vector2 and if the distance is < some value you create a new random range

1 Like

I actually started to unity and c# in 3 days ago, so what you are saying is makes sense in my mind but can’t actually turn it to code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnTest : MonoBehaviour {
    public int numberToSpawn;
    public List<GameObject> spawnPool;
    public GameObject quad;
    public float minDistance = 2.0f;
   
    private MeshCollider collider;
    private List<Vector2>spawnPoints;
   
    void Start()
    {
        spawnObjects();
    }
    public void spawnObjects()
    {
        spawnPoints = new List<Vector2>();
        collider = quad.GetComponent<MeshCollider>();;
       
        int randomItem = 0;
        GameObject toSpawn;
       
       
        for (int i = 0; i < numberToSpawn; i++)
        {
            randomItem = Random.Range(0, spawnPool.Count);
            toSpawn = spawnPool[randomItem];
            Vector2 pos = randomSpawnPoint();
            if(pos == Vector2.zero) {
                break;
            }
           
            spawnPoints.Add(pos);
            GameObject newObject = Instantiate(toSpawn, pos, toSpawn.transform.rotation);
            newObject.GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
        }
    }
   
    private Vector2 randomSpawnPoint() {
        Vector2 pos;
        float distance = 0;
        // let's try 10 times to find a spot that is not occupied
        for(int i = 0; i < 10; i++) {
            pos = randomPointOnCollider();
           
            bool newPosIsFarEnoughAway = true;
            foreach(Vector2 spawnPoint in spawnPoints) {
                distance = Vector2.Distance(pos, spawnPoint);
                if(distance < minDistance) {
                    newPosIsFarEnoughAway = false;
                    break;
                }
            }
            if(newPosIsFarEnoughAway) {
                return pos;
            }
        }
       
        // we could not find a pos that is far enough away
        return Vector2.zero;
    }
   
    private Vector2 randomPointOnCollider() {
        float screenX = Random.Range(collider.bounds.min.x, collider.bounds.max.x);
        float screenY = Random.Range(collider.bounds.min.y, collider.bounds.max.y);
        return new Vector2(screenX, screenY);
    }
   
    private void destroyObject()
    {
        foreach (GameObject o in GameObject.FindGameObjectsWithTag("Food"))
        {
            Destroy(o);
        }
    }
}
2 Likes

What @BenniKo posted is great and I use stuff like that all the time.
But just for future reference - that method doesn’t scale well at all in my experience, you might wanna look into a little more advance stuff like poisson disk sample. (the creator of this video has excellent tutorials too, btw)

1 Like

I totally agree with @SparrowsNest . The poisson disk sample is the way to go as soon as @gamingfail35 has at least 6 days of unity experience :slight_smile:

1 Like

T

Benniko’s code worked for me but i’ll also take look at the videos because i need lots of tutorials. I started with 0 experience, its now 3 days in and i have a little snake like mobile game. Thanks for all the replies :slight_smile:

2 Likes

Good luck on your journey!
You should really take a look at this and this playlists by the same guy I linked, he’s an excellent teacher.