- Home /
Trying out a unique method of spawning enemies. Someone want to help sort out the logic of it?
I've got a linear-style game where you scroll through a level all the way to the end automatically without having to hand-place them.
My idea for spawning enemies works a little bit like this:
- Each level has some sort of "LevelSpawn" script.
- In it, I can set the number of enemies that will spawn by using the inspector to set the number of elements in a GameObject array.
- I'll fill in all the objects with the prefab enemies I wish to spawn, in order of appearance.
- I'll then use another array of floats. These floats would determine WHEN each enemy in the list will spawn. So if Time[0] is 1.0F, then SpawnedEnemy[0] will be created.
- Finally, I need a value to say where, horizontally, each enemy will spawn (they'll move downward by themselves from their own AI).
I'm having trouble thinking of how to string all this together and make it work.
Should I be going for a bunch of 1-dimensional arrays and finding ways for them to talk to each other, or should I be utilizing a 2-dimensional array somehow?
What I have right now is:
public GameObject[] EnemiesToSpawn;
public float[] TimeToSpawnEnemies;
But I'm not sure how I'm going to actually get this running smoothly. I thought I would have a timer variable, and constantly check to see if it's time to spawn a new enemy, but I don't know how I would turn it off and avoid repeatedly spawning an enemy after his time has come to appear (thus probably freezing the game...).
Should I think of another way of doing this entirely? If so, what do you suggest?
Answer by kennypu · Apr 15, 2011 at 11:23 PM
I think the way you're doing is fine. But to keep things neat, I would suggest creating a new class to store all the information for each of the enemies, or have a multidimensional array where you can just push all those values into. I'll be using javascript (sorry), but you should get the idea/be able to convert it easily: public var enemyList:Array(); public var enemyInfo:Array();
//make all your enemies. info is in order of prefab, spawnTime, and spawn location enemyInfo = new Array(prefab, 2.3, Vector3(0,0,0) ); enemyList.push(enemyInfo);
//the next enemy enemyInfo = new Array(.....); enemyList.push(enemyInfo); . . . //now when reading, you just have to check: enemyList[0][0]; //this is the prefab enemyList[0][1]; //this is the time enemyList[0][2]; //this is the location
this can obviously be very hassling, so it would be cleaner to make a class, but thats up to you. Others may provide better answers. This is just the idea I thought of off the top of my head.
Why split it out? He should do as you said, but then also use that array of info directly.
Your answer
Follow this Question
Related Questions
delay first spawn 2 Answers
Spawning Objects at location 3 Answers
How do i spawn an enemy but with a randomized direction everytime it spawns? 1 Answer
Problem with the enemy 0 Answers
enemy wave spawn script wont work 0 Answers