Level generator

For the level pattern in unity 2D games,  i could generate all enemies before hand the moment the game started. However it requires lots of memory and becomes really hard to manage, even slows down your game if the level is too long. So we should generate each GameObject dynamically, it becomes easy to manage and change.

There are plenty of tutorials online of how to design a powerful level generator with all-rounded functions. What i did in the Aetherial project is a simple and slight instance which achieves the basic function using C# language. In general, it consists of two classes and one method to call. I set a child empty GameObject to “GameController” object on hierarchy named “EnemySpawner” which holds two scripts.


The first script is a empty class with 2 functional classes, “SpawnGroup” and “EnemyInstance” the SpawnGroup is basically holding a list of EnemyInstance, a bool which indicate if this group of enemies are created or not and a float represents the time interval after the last group.  And the EnemyInstance class Contains a bool to indicate if the enemy has been created, a float of the interval between each enemies, a int of the type of enemy and a Vector2 represents the spawn position.

After we use [System.Serializable] and set a public list of the SpawnGroup we could manually decide every details of the spawning pattern in the inspector. All the properties are dynamically variables and flexible to change. Even you could take it as an interface to let designer decide details easily.

The final method to call is kind of tricky. Since the game is a liner game, you can’t speed up or slow down the process, the spawning pattern are all based on time past on game. So In the last script “EnemySpawner” I have a float m_CurrentTime to store the current time and use (Time.time – m_CurrentTime) to calculate the intervals of enemies spawning and groups. And an integer of the current group index. So, In the Update, i detect the index number of the group. If the state of the group is false them we start to search the states of the EnemyInstance inside the group. If Interval reaches the SpawnWait properties, then i switch the type of the instance, Instantiate the concrete object in the Vector2 location and set m_CurrentTime = Time.time; which resets the interval back to zero.

3

In this way, I get an fluent spawning wave of enemies. Also after added the index of SpawnGroup it’s easy to add check point in game, you only need to set SpawnGroup back to a certain number instead of zero.

4

About Hangning Zhang

2017 Programming