How to know if there is no possible space on board to spawn.
I have a method that spawns 4 different type of prefabs in order of priority. it spawns the first type, then spawns the next if there is room. There's a number, of objects per type to be spawned, specified. I want it done in a way that if there's no room for an object of lower priority to spawn, then it doesn't spawn. This is the method I have:
void Spawn( GameObject obj, float height, int num, int max){
currentObjectsOnBoard = 0;//number of specified objects spawned
for (int i = 0; i < max; i++) {
if (num == 0) {
break;
}
float x = Random.Range (startX, (startX + boardW));
float y = height;
float z = Random.Range (startZ, (startZ - boardH));
Vector3 temp = new Vector3 (x, y, z);
Collider[] hitColliders = Physics.OverlapSphere(temp, spacer);
int a = 0;
bool free = true;
while (a < hitColliders.Length)
{
if ((hitColliders [a].CompareTag ("Collectable")) || (hitColliders [a].CompareTag ("Obstacle")) || (hitColliders [a].CompareTag ("Hole")) || (hitColliders [a].CompareTag ("Player")) || (hitColliders [a].CompareTag ("Coin"))) {
free=false;
//check if spot is occupied by any of the above, within a radius specified by spacer
}
a++;
}
if (free) {
Instantiate (obj, temp, Quaternion.identity);
currentObjectsOnBoard++;
} else {
i--;//i did it like this so the maximum number of spawnable items can be fully reached
}
if ((currentObjectsOnBoard == num)) {
break;
//if object reaches number specified or if no more can be spawned.
}
}
}
How I implemented this was before each object is spawned, I calculate the maximum number of that object that can be spawned after objects of higher priority have been spawned...I think my problem is my math. I think I'm not calculating the maximum number spawnable right. I use the are of each object assuming this was a flat 2d board and I multiply that number by the space I want in between all objects. Here's how I implemented the first two:
maxCollectablesOnBoard = (int) (((boardArea- (playerArea)) / (collectArea)));
Spawn (collectable, collectAndCoinHeight, collectNum, maxCollectablesOnBoard);
tempcollect = currentObjectsOnBoard;//number of collectables spawned if different from numer requested
maxHolesOnBoard = (int) (((boardArea - (playerArea) - (tempcollect * collectArea)) / (holeArea)));
Spawn (hole, holeHeight, holeNum, maxHolesOnBoard);
here's an example of how I calculated the area:
collectArea = (spacer+1) * (Mathf.PI * collectRad * collectRad);
I've been trying this for days and I can't figure it out, I'd really appreciate the help. Thank you!
Answer by Cuttlas-U · Apr 17, 2017 at 06:41 AM
hi; i cant really help u with this based on your script and the way u did it;
if it was on me i would do 2 Instantiation first one is just temperory and create an object of the same prefab without meshrenderer but has collider ; this Instantiation has a script on it that works as detector see if it has collision with any thing or not ; if not it will send a request to the main script and send the order to Instantiate the real object and destroy him self cause its notneeded any more;
Hello, I''m sorry I didn't reply, I sort of gave up on the game cause I couldn't figure this out but I will try out your solution and reply if I have any further problems with it. Thank you so much
Your answer
