- Home /
Enemy Spawning in Infinite Runner
I am trying to develop a system of randomly spawning enemies in an infinite runner. So, here is the process in which I am going to spawn the enemies. I am going to choose a time interval at which the enemy is spawned at based on difficulty. (time between each enemy spawning gets shorter as the infinite runner game goes on. Then I am going to randomly choose the distance apart the enemy will spawned at based on difficulty. Then choose from 4 different enemy choices. What I just explained is my grand, master plan, but I am still trying to just spawn one enemy let alone all 4. I have been reading all about prefabs, instantiation, object pooling, and reusing objects. What I have learned is that instantiation and destroying objects can be expensive, but so can object pooling at times. I am 13 years old and I have just begun developing in Unity so object pooling from what I can see is a pretty complex topic. You are probably more experienced than me so, what do you think is the best path to go down for what I am trying to do? This is not a write-me-the code question, it is just asking what path I should follow to spawn the enemies. Here is the code I have so far which generates enemies but gets to a point where the game gets so slow, it is unplayable.
public Transform garbagePile;
private float randomeTimeInterval;
private float randomXDistance;
private float lastXValue;
private int randomZPos;
private float[] randomZValues;
private float theZValue;
private int numOfGarbage;
// Use this for initialization
void Start() {
CreateEnemy ();
randomZValues = new float[3];
randomZValues [0] = 0.0f;
randomZValues [1] = -2.0f;
randomZValues [2] = 2.0f;
}
void CreateEnemy () {
randomXDistance = Random.Range (6, 9);
randomZPos = Random.Range (0, 3);
theZValue = randomZValues [randomZPos];
Vector3 pos = new Vector3((float)lastXValue + randomXDistance,0,(float)theZValue);
Instantiate(garbagePile, pos, Quaternion.identity);
lastXValue = garbagePile.transform.position.x;
}
// Update is called once per frame
void Update () {
Vector3 updatePos = new Vector3 ((float)0.2, 0, 0);
garbagePile.transform.position -= updatePos;
if(garbagePile.transform.position.x <= (float)-11){
Destroy(gameObject);
CreateEnemy();
}
}
}
Answer by dorpeleg · Feb 20, 2014 at 10:24 AM
You should use pools.
The complexity of pools depends on your game, for now its seems like this should be an easy pool system.
Here the basic of pooling system:
Decide on a maximum objects you want the pool to have.
When you want to create an object, first, check if he is in the pool.
If he is in the pool take him from there, if not, instantiate him and add to pool.
When you want to destroy an object, you simply disable him but keep him in the pool.
So actually, each time you try to create a new object, you need to test the following:
Is there a copy of the object I'm trying to create already in pool? if not, instantiate and add to pool.
If yes, is that object currently disabled? if not, instantiate and add to pool.
If yes, enable the object and move it to where you want (no instantiation).
Your answer
Follow this Question
Related Questions
bullets kill all enemies, not just the ones hit. 1 Answer
Destroy and Spawn an Enemy 1 Answer
making infinite road with prefabs 1 Answer
enemy ai going to player 2 Answers