In “How to Build a VR Shooting Game”, I am going to show you how to build a VR shooting game using Unity and Playmaker, along with how to upload it to STYLY and publish it.
In this article, I will show you how to set up Enemy Counter to manage the number of enemies and Spawner to generate enemies, and how to build a system to manage the number of enemies.
- Advance preparation
- Gun Controller Settings
- Setting up the bullets
- How to generate enemy characters
- Enemy Character Settings
- BGM and SE Settings
- How to upload to STYLY
Part.1-7 continues, so let’s read and make it to the end!
Last time, we set up the bullet for the gun used in the game.
- 1 How to set up the Enemy Counter
- 2 How to set up EnemySpawner
- 3 How to spawn enemies at random locations
- 3.1 Create an enemyCount variable
- 3.2 Creating a Character Generation state
- 3.3 Create an Automatic generation event
- 3.4 Adding the Random Float action
- 3.5 Add the Set Vector3 XYZ action.
- 3.6 Add Creat Object action
- 3.7 Added the Send Event action.
- 3.8 Added the Randam Wait action.
- 3.9 Create a Checking Number of Enemies state
- 3.10 Add the Get Fsm Variable action.
- 3.11 Add Int Compare action.
- 4 Sample Scene
How to set up the Enemy Counter
We will create a counter object to manage the number of enemies on the screen.
Create an enemyCounter
Let’s create a new empty game object in the Scene Hierarchy and name it “enemyCounter”.
Create an FSM
Create an FSM for the enemyCounter and name the start state “Setup”.
Create a global event
Create two events in this FSM with namely “addEnemy” and “subtracetEnemy” and check the box next to their names to make them global events.
This makes it accessible to other objects in the game that have FSMs.
Add a global event with the corresponding name to each.
The FSM should look like the following figure.
Add an Int Add action
Add the “Int Add” action to the Adding Enemy state and set Add to 1 so that it is added to the enemyCount.
This increases the count by 1 if an enemy is spawned.
We will add an Int Add action to the Subtracting Enemy state as well, but we will set the value to -1.
This reduces the number of enemies in the scene by one.
How to set up EnemySpawner
Create an enemySpawner
Next, create an empty game object in the Scene Hierarchy and name it “enemySpawner”.
Note further that it is a child element of the PlayMaker Tutorial Game.
It will then be included in the prefab to be exported to STYLY later.
Create an FSM
Create a new FSM in enemySpawner and name it as “Setup”, the first state, as shown in the figure below.
Create a maxEnemies variable
We create a new variable called “maxEnemies” in this start state.
This is used to determine the maximum number of enemies that can be spawned on a stage.
In this case, the value of Value is set to 20 and the variable is set to Int so that no more than 20 objects are generated.
Adding the Get Fsm Variable action
We will also create a new “Get Fsm Variable” action in the Setup state for enemySpawner, as shown in the following image.
Select the Specify Game Object from the Game Object drop-down menu and drag it from the Scene Hierarchy to the enemyCounter object to designate the Game Object as an enemyCounter.
Store the variable maxEnemies in Store Value.
Now that the Enemy Counter and EnemySpawner are set up, we have a mechanism to manage the number of enemies.
How to spawn enemies at random locations
Create an enemyCount variable
Next, let the enemies spawn from a random position on the stage.
First, open the enemySpaner FSM and create a new Int variable called “enemyCount”.
Creating a Character Generation state
Next, we create a transition to a new state called “Character Generation” upon completion of the Setup state.
Create an Automatic generation event
Create a new event called “Automatic generation” and add it to the Character Generation state.
Adding the Random Float action
We add two new “Random Float” actions in the Character Generation state and store them in new variables called “X” and “Z”, as shown in the figure below.
By setting up variables like this, you can make enemies spawn randomly from the back of the stage.
Add the Set Vector3 XYZ action.
Add the “Set Vector3 XYZ” action and have the X and Z random variables stored in the “EnemyPos” variable, as shown in the figure below.
In this case, the value of “Y” should be set to 1 in order to show the enemy standing on the stage.
Now you can set the position variable to generate enemies randomly.
Add Creat Object action
Add a “Create Object” action to spawn an enemy.
Set Enemy (Enemy will be touched on in future articles, it is not necessary at this moment) to the Game Object and set the EnemyPos variable that I made earlier to Position.
Added the Send Event action.
Add a “Send Event” action and configure it as below.
This spawns a global event addEnemy, which causes the count to increase when an enemy is spawned.
Added the Randam Wait action.
Add a “Randam Wait” action and set the Finish Event to Automatic generation.
By setting it as shown below, a 1-3 seconds waiting time will occur after the enemy is spawned.
Create a Checking Number of Enemies state
Create a “Checking Number of Enemies” state and add a new event called a spawn enemy to the state.
Add the Get Fsm Variable action.
Add a “Get Fsm Variable” action in the Checking Number of Enemies state to specify the Game Object as an enemyCounter.
Since we want to get the enemyCount variable this time, we will set Store Value to enemyCount.
Creates a transition from the Checking Number of Enemies state to the Setup state.
Add Int Compare action.
Finally, we add the “Int Compare” action and set it up as shown below. Make sure that the “Every Frame” box is checked.
This compares enemyCount to maxEnemies and triggers a spawn enemy event when enemyCount is low, causing it to spawn an enemy.
As maxEnemies was set as 20 earlier, it will only spawn when the number of enemies is between 0 and 19.
By completing the above, you can build a system to manage the number of enemies and let them spawn from random positions.
In the next article, I’ll show you how to set up an enemy girl.
You can try out the sample game in the STYLY GALLERY.