Hi, I have a really simple game world and I am trying to spawn a range of building prefabs in a random position and rotation. They do that okay, but they will sometimes spawn inside each other and because of the rigidbodies and colliders they tend to explode.
So I think I need something that checks where the prefabs are going to be spawned and makes sure that none of the colliders would cross over, but if they do try spawning the prefab in a different position.
public GameObject[] WorldObjects;
public int amountToSpawn;
public Vector3 spawnValues;
void Start()
{
GenerateWorldPrefab();
}
public void GenerateWorldPrefab()
{
for (int i = 0; i < amountToSpawn; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
Quaternion spawnRotation = new Quaternion(0, Random.Range(0, 360), 0, 90);
int r = Random.Range(0, WorldObjects.Length);
Instantiate(WorldObjects[r], spawnPosition, spawnRotation);
}
}
This is what I have so far.
Any help or suggestions would be great, thanks.