- Home /
How to spawn random enemies at a free random spawn point?
Hi I'm trying to have random enemies spawn at random free spawn points. I've got it to work to the point where I can spawn random enemies at random spawn points but having hard time scripting it to only spawn at a free spawn points.
This is the script for checking for a free spawn points.This is the first time I'm trying to use Arraylist and I'm totally lost :(
var spawnPoints : Transform[]; var enemyPrefabs : GameObject[]; var amountEnemies = 20; var yieldTimeMin = 1; var yieldTimeMax = 3;
private function AvailableSpawnPoints() : GameObject[] //checks for available spawnpoints
{
var gos : ArrayList = new ArrayList();
for( cnt = 0; cnt < spawnPoints.length; cnt++ )
{
if (spawnPoints[cnt].transform.childCount == 0 )
{
gos.Add(spawnPoints[cnt]);
}
}
return gos.ToArray();
}
And this is to actually spawning enemies,
function Spawn() {
var gos : GameObject[] = AvailableSpawnPoints();
for (i=0; i< gos.length; i++) { yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)];
Instantiate(obj, gos[i].transform.position, Quaternion.Identity);
obj.transform.parent = gos[i].transform;
} }
Any ideas? Thanks.
Answer by schwertfisch · Jan 19, 2011 at 09:40 PM
Have a similar problem. Here's what I've used till now.
I have 2 arrays, one comprised of the possible collectibles to be spawned randomly and another comprised of the empty tiles (where a spawning can occur).
The problem is that the game is an iOS game and the way I update the contents of the empty tiles array is cpu-intensive and the game is pretty much unplayable at the moment.
If you're targeted to pc/mac/web though, this method works fine.
Your answer
