ı 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);
}
}
}
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)
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