Game Toolkit for Unity
Setting up the game controller spawning the enemy waves.
The game controller is responsible for spawning the enemy waves. For now, we’ll only use the 3 asteroids as enemies – enemy ships will be added later. The game controller will run in a repeating auto machine component and use Wait nodes to wait between spawning enemies and waves.
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
We’ll set up local variables as Machine Start Variables for easy setup in the machine component at a later time. When using the schematic in a machine component, the defined start variables will be added automatically, using their default values.
Click on Add Start Variable to add a local start variable that will be exposed to the machine component’s inspector.
Again, click on Add Start Variable to add another local start variable.
We’ll add the asteroid prefabs as a single prefab resource to randomly spawn one of them. Click on Add Prefab Resource to add a prefab resource. Make sure that Destroy Prefabs is disabled.
Change the first prefab’s settings.
Click on Add Prefab to add another prefab to the resource.
Add Node > Value > Float > Float Random
This node will store a random float value into a variable. We’ll use it to create a random X-position for the spawning enemy.
Add Node > Game Object > Prefab > Spawn Prefab
We’ll spawn a prefab using the random X position we just created.
The X position will use the random float value we created.
The Y position will use the Y position of the local start variable spawnPosition.
The Y position will use the Z position of the local start variable spawnPosition.
Add Node > Machine > Is Schematic Stopped
This node checks if the schematic is stopped, e.g. due to the machine object being destroyed. We’ll use it to prevent looping through the enemy spawns when the level is reloaded after game over.
There are no additional settings.
Add Node > Base > Loop
The Loop node is a useful node to repeat a part of your schematic for a number of times. In short, it’ll increase a counter variable (int) by 1 each time it’s used until it reaches a defined maximum value.
This node is connected to the Running slot of the Is Schematic Stopped node.
Add Node > Base > Wait
This node will wait for a defined amount of time. We’ll wait for a random time between 0.5 and 1 seconds before spawning the next enemy.
This node is connected to the Loop slot of the Loop node and connects back to the Float Random node. This will repeat spawning a prefab as long as the count hasn’t reached the required number.
We’ll wait for a random time between 2 and 5 seconds until the schematic ends (which will restart the machine, spawning the next wave).
This node is connected to the Finished slot of the Loop node.
And that’s it for the schematic – click on Save Schematic and save it as GameController in Assets/Schematics/.
Now, we’ll add the Game Controller schematic to the scene. Create a new empty object (e.g. using the Unity menu: GameObject > Create Empty), rename it to Game Controller and place it at X=-10, Y=0, Z=2.
Add an auto machine component to the game object (e.g. using the component menu: Makinom > Machines > Auto Machine) and change the following settings.
The Machine Start Variables we’ve set up in the schematic are automatically added here with their default values – since the default values are already what we need, we don’t have more to do here. Make sure all variables are enabled.
And that’s it for now – don’t forget to save the scene.
Click on Play to test the game. Now, the game controller will spawn waves of 10 asteroids (spawning randomly one of the 3 asteroids) and you’ll be able to destroy them by firing bolts on them. When the player is hit, the game will just keep running with the player being gone – we’ll take care of this later.
The next tutorial will handle the UI setup.