[Unity/Playmaker] How to make a VR shooting game and generate enemy characters

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.

  1. Advance preparation
  2. Gun Controller Settings
  3. Setting up the bullets
  4. How to generate enemy characters
  5. Enemy Character Settings
  6. BGM and SE Settings
  7. 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.

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”.

enemyCounterを作成

Create an enemyCounter

Create an FSM

Create an FSM for the enemyCounter and name the start state “Setup”.

enemyCounterのFSMを作成

Create an FSM for enemyCounter

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.

チェックを入れグローバルイベント化する

Check the box to make it a global event.

Add a global event with the corresponding name to each.

The FSM should look like the following figure.

enemyCounterにグローバルイベントを追加

Added a global event to enemyCounter.

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.

Adding EnemyステートにInt Addアクションを設定

Setting an Int Add action for the Adding Enemy state

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.

Subtracting EnemyステートにInt Addアクションを追加

Adding an Int Add action to the Subtracting Enemy state

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.

enemySpawnerを追加

Add enemySpawner

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.

enemySpawnerにFSMを追加

Added FSM to enemySpawner.

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.

maxEnemies変数を作成

Create a maxEnemies variable

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.

Get Fsm Variableアクションを追加

Added the Get Fsm Variable action.

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”.

enemyCount変数を作成

Create an enemyCount variable

Creating a Character Generation state

Next, we create a transition to a new state called “Character Generation” upon completion of the Setup state.

Character Generationステートを作成

Create a Character Generation state

Create an Automatic generation event

Create a new event called “Automatic generation” and add it to the Character Generation state.

Automatic generationイベントを作成

Create an Automatic generation event

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.

Random Floatアクションの設定

Setting the Random Float action

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.

Set Vector3 XYZアクションの設定

Set the Vector3 XYZ action.

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.

Creat Objectアクションの追加

Adding a Creat Object Action

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.

Send Eventアクションの追加

Adding the Send Event action

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.

Randam Waitアクションの追加

Adding the Randam Wait action.

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.

Checking Number of Enemiesステートを作成

Create a Checking Number of Enemies 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.

Get Fsm Variableアクションを追加

Added the Get Fsm Variable action.

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.

Int Compareアクションを追加

Add an Int Compare action.

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.

Sample Scene

You can try out the sample game in the STYLY GALLERY.