- Home /
How to make objects spawn at the same time without colliding ?
I'm trying to make a spawner that spawns spheres that move in a straight line as soon as they are spawned, and the number of spheres increases by waves. But the Spawn Manager script makes them spawn so close to each other that they colide and change course. I tried folowing the concept in the solution on this question: https://answers.unity.com/questions/1506835/how-to-prevent-3d-object-spawn-overlapping.html But I couldn't incorporate in my code, it would never enter the loop or keep adding to "wave" infinitely.
If anyone could help me with this I'd be very thankfull.
This is my orginal script: using UnityEngine;
public class SpawnManager : MonoBehaviour
{
private float spawnBounds = 80.0f;
public GameObject soulPrefab;
public int soulCount;
public int wave = 0;
// Start is called before the first frame update
void Start()
{
SpawnSouls(wave);
wave++;
}
// Update is called once per frame
void Update()
{
soulCount = FindObjectsOfType<MoveSouls>().Length;
if (soulCount == 0)
{
SpawnSouls(wave);
wave++;
}
}
void SpawnSouls(int numberOfSouls)
{ // spawning possiton variables
float randomX = Random.Range(-spawnBounds, spawnBounds);
Vector3 randomSpawnPosition = new Vector3(randomX, 5, 61);
for (int count = 0; count < numberOfSouls; count++)
{
Instantiate(soulPrefab, randomSpawnPosition, transform.rotation);
}
}
}
This is the script after traying the solution:
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
private float spawnBounds = 80.0f;
public GameObject soulPrefab;
public int soulCount;
public int wave = 0;
int maxSpawnAtempsts = 20;
public int spawnAtempts = 0;
private bool validPosition = false;
public float checkRadius = 1.0f;
// Start is called before the first frame update
void Start()
{
SpawnSouls(wave);
wave++;
}
// Update is called once per frame
void Update()
{
soulCount = FindObjectsOfType<MoveSouls>().Length;
if (soulCount == 0)
{
SpawnSouls(wave);
wave++;
}
}
void SpawnSouls(int numberOfSouls)
{ // spawning possiton variables
float randomX = Random.Range(-spawnBounds, spawnBounds);
Vector3 randomSpawnPosition = new Vector3(randomX, 5, 61);
for (int count = 0; count < numberOfSouls; count++)
{
while (!validPosition && spawnAtempts > maxSpawnAtempsts)
{
spawnAtempts++;
validPosition = true;
Collider[] coliders = Physics.OverlapSphere(randomSpawnPosition, checkRadius);
foreach (Collider col in coliders)
{
if (col.tag == "Soul")
{
validPosition = false;
}
}
if (validPosition)
{
Instantiate(soulPrefab, randomSpawnPosition, transform.rotation);
}
}
}
}
}
Answer by Cris_2013 · Aug 02, 2020 at 08:58 AM
Without looking at your code, you can solve your problem in a variety of ways... you can obviously check for collision in order to detect if there is already an sphere on the spawn position and if so, wait until the spawn position is empty to spawn a new sphere. However that wold the point to note here is that no matter what, if you only have one spawn position and you need to spawn an high number of spheres in a short period of time, you will always have the same problem.
Games solve this in a few different ways that have more to do with design or level design than with coding:
Have multiple spawn positions. Each wave properties can have a list of what spawn positions should spawn spheres (so you control the position spheres come from).
Spawn position have spawn slots nearby, so you spawn always in a spawn slot which is enough apart from each other to avoid collisions.
Spheres ignore collide with each other (although this can go against the design of your game).
Have a sphere queue in your spawn position and a cool-down timer that controls how often a sphere can be spawned.
If you are creative you can solve the issue without the need of complex code :)
Thatnks! I was so focused in the code that I didn't even think about it in a desing sense.
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Collision check on Placement 1 Answer
Distribute terrain in zones 3 Answers
overlapping object doesn't disable 0 Answers
Changing text values every time you instantiate the button 1 Answer