The goal for this article series is to use PlayMaker in Unity to create a FPS shooting game, and export it to the STYLY editor.
There are 7 articles for this series, so if you roughly look through the series first, I think you will get the general flow of the tutorial. In this article, we will learn how to setup the enemy counter and spawner.
 Setting up the Enemy Counter and Spawner
Let’s create a counter object to manage the enemy numbers in the screen.
By creating managing objects, we can create a organized structure to sort out and relate the many objects in the game, and enable high level system editing without the need to change the setting of multiple game object FSM’s.
First, create an empty game object in the Scene Hierarchy, and name it “enemyCounter”.
Add a FSM to “enemyCounter”, and although we will not use the start state, name it Setup for consistency.
Create two events named “addEnemy” and “subtractEnemy”, and check the box next to them to make them global events, to enable access from other objects with FSM’s.
Create two new states with the same names, then add the newly created global events as transitions.
Add an “Int Add” action to Adding Enemy, set Add to 1, and for int Variable, create a new variable called enemyCount, in which we will store the number of enemies.
Do the same for the Subtracting Enemy state, but set Add to -1, since we want to subtract the enemy count.
Click on the Variables tab and create a new variable called maxEnemies. Eat the value to 20. We will use this later, for the purpose of limiting the game lag.
Make sure maxEnemies is set as an Int.
Create a empty game object in the Scene Hierarchy as a child of PlayMaker Tutorial Game, and name it “enemySpawner”. By making it a child, it will be included in the prefab we will be exporting to STYLY.
Create a new FSM in “enemySpawner” and name the entry state “Setup”. Create a new int variable and name it “maxEnemies”. Make sure that it is the same name as the maxEnemies variable in the enemyCounter FSM. You will soon realize why.
In the enemySpawner Setup state, create a Get Fsm Variable, just as in the image below.
Select “Specify Game Object” from the Game Object dropdown menu, drag and drop the enemyCounter object from the Scene Hierarchy, and set the Game Object to enemyCounter.
Then, we need to set the value to save in the maxEnemies variable in enemySpawner. The reason for this is because STYLY does not support the use of playMaker global variables. Global variable are usually used to exchange data between multiple FSM’s connected to different objects. For our example, we need both enemy counter and enemy spawner to know the max number of enemies, so we need some kind of function that works like a global variable.
But since we cannot use global variables, we will forcefully hack the Get Fsm Variable Action to achieve this. With this action, we will copy the value of a variable to another variable of a different object but with the same name. For this program, that name will be maxEnemies. We can now change one value in order to change the maxEnemies value for both. Simplifying programming is a strong reason to use variables.
Next, create a new int variable and name it “enemyCount”. This will be used to count the number of enemies.
Add a FINISHED transition from Setup to a new state called Waiting, and add a FINISHED transition from Waiting to a new state called Checking Number of Enemies.
Add a Get Fsm Variable action to the “Checking Number of Enemies” state, and set the game object as “enemyCounter”. This time, we want to get. The enemyCount value, so set Store Value as enemyCount. The two variables “maxEnemies” and “enemyCount”, will be shared between “enemyCounter” and “enemySpawner”.
Create a new event called “spawn enemy” and add it to the Cheking Number of Enemies state.
Create an Int Compare action to compare enemyCount and maxEnemies, and set it so when enemyCount is less than maxEnemies, the spawn enemy event will be triggered.
Also, make sure that the “Every Frame” box is checked, so that the game will check how many enemies there are every frame.
Add a transition to the Spawning Enemy state. When it finishes, make it transition back to waiting.
In order to randomly create enemies in the circle, there is need for a little calculation.
First, create a new float variable named “radius”, and set the value to 76. This is the radius value we want for the range of the circle for spawning enemies.
Go to the Scene Hierarchy, and set the Transform component of enemySpawner to 0,0,0. This will set the circle in the arena center. This will make the enemies spawn in an equal distance from the player.
To select a certain point on the edge of the circle, we use simple trigonometry.
For example, if we use a circle as in the image below:
If the red and blue cross point is (0,0), we can use sin and cosine to calculate the position of the tip of the red line.
x = rcosθ
y = rsinθ
The round arena in the game already has a radius, and the vector start point is (0,0). This is where we set the enemySpawner, so we can use the same calculations to get a certain point on the circle edge.
What we need to do here is to select a random angle θ. Then, we calculate the x-axis and y-axis of θ.
The steps we need in playMaker are explained in the image below.
First, we need a random floating-point number. This will work as a random variable. Calculate the cosine for this variable, and store it to a new variable named “xCoord”. The cosine of playMaker needs to be calculated with radian units, so don’t forget to check the Deg to Rad transform box. Multiply the xCoord with its radius. Use the same randomTheta variable to get the sine. You need to change the units to radians here too. We now have a random position generator to use for spawning enemies.
The last step is the store those values to a vector variable named “position”, and since in Unity the Z-axis exits parallel to the ground, set x as the xCoord, and z as the yCoord. As in tradition, I used x and y for circle geometry. Set the vector y value to 1. By these settings, we can set the enemies on the correct height, and make them look as if they are standing on the stage.
So, we add a Create Object action, so that the object is shown in position. Random points on the circle will be stored as vectors.
Leave the Game Object field blank. We will create the prefab for enemies, and eventually set it here.
We also need to renew the enemy counter. Check that you have created an add Enemy global event in enemyCounter, and call the event from the enemySpawner object’s FSM, and that the enemy count is increases.
Click Event Browser, located below enemySpawner, and add an addEnemy event.
Next, click the Spawning Enemy Event, add a Send Event action, and send the addEnemy event to the enemyCounter object.
Except for adding the enemy prefab later, settings of the counter and the spawner system is finished. In the next article, let’s start creating enemies.
・Using PlayMaker to create a game, and exporting it to STYLY -1-