- Home /
Preventing Spawn Overlap With Spheres
Hello, I am new to Unity. I am unfortunately still quite confused on how to solve the overlapping of Spawned objects. Help would be greatly appreciated :) I have watched many tutorials and have read many answers but cannot seem to get it.
public class PaintballSpawner : MonoBehaviour
{
public GameObject[] ballPrefabs;
public float spawnRangeZ = 6;
private float spawnInterval = 5f;
private float startDelay = 2;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("SpawnRandomPaintBall", startDelay, spawnInterval);
}
void SpawnRandomPaintBall()
{
Vector3 spawnPos = new Vector3(-1, 0, Random.Range(-spawnRangeZ, spawnRangeZ));
int ballIndex = Random.Range(0, ballPrefabs.Length);
Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs
[ballIndex].transform.rotation);
}
}
Does that make sense/ help? Would you like an example?
Answer by sacredgeometry · Jan 31, 2020 at 08:44 AM
Assuming you want to pack them regularly:
Assuming you just don't want them to touch surely its just a matter of checking that the ball is at least infinitesimally more than the radius apart from any of the other instances. Are you packing them regularly or wanting them to pack in an irregular way?
Another way to do pseudo random position (its a far less complex thing to compute too) it is to pack them regularly with padding and to use that padding as your randomness space.
I guess the question is what are you trying to make them look like i.e. what is the purpose of the randomness?
The balls are being spawned along a line at random positions. There are different types of these sphere objects they do different things depending on which one the player gets.
The method still applies surely, or am I missing something?
I just have no idea how to write it code wise to check if a sphere is there, and where to put it in the code lol.
I mean either that or you could evaluate forwards through the list and store the position and size of the last item in it.
I can write you an example of both with a description tomorrow
It's ok :) I am going to watch more of the Unity course stuff. Thank you for offering.
Your answer
Follow this Question
Related Questions
How to avoid overlapping ground spawns?,How to avoid overlapped ground spawns 0 Answers
Objects overlapping at origin (0,0,0) (question about OverlapSphere?) 0 Answers
How to respawn player car in war track 1 Answer
Detect object outside overlapSphere 0 Answers
Question about Unet and spawning 0 Answers