Using PlayMaker to create a game, and exporting it to STYLY -2-

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 gun controller.

[5] How to setup a gun controller

I will also introduce the basics of the FSM (Finite State Machine), events, and actions. These are the core concepts when creating playMaker games.

Create a empty child object in PlayMaker Tutorial Game, and name it gunController_R. This is where you are going to place the right hand gun controller aspect. After we setup the initial settings, we will duplicate it, and custom the settings to create a left hand gun controller.

(By the way, in the following sample images, there will be an object named playmaker GameFinished in the Scene Hierarchy, but this object was created just for this tutorial, so do not get alarmed that it doesn’t exist in your project)

For the next step, with gunController_R selected, right click in the Playmaker editor and select “Add FSM”.

A FSM is a system which gets active upon transition between states. We need to setup States, which show the current state of an object, and Events where objects change to one state from another.

An good example would be the opening and closing of a door. The states we need for this action are “open” and “close”, and the events would be “open door” and “close door”. We will learn the basic concepts of states and events through this tutorial, and understand how they are used in playMaker.

So, lets get back to the Unity screen, right click again, and select [Add State] from the menu to add a new state to the game. Lets name the state “setup”. By finishing this step, we have created the basic settings for the gun controller object.

In the Actions window, search for the “Find Game Object” Action, then click to add it to our state.

Next, search for “Controller right” in the Object Name bar, store it in a new variable, and call it “controller”.

This action”s aim is to find the appropriate controller from the scene, store the variable needed in the FSM, and run an certain action. This works just like in normal programming, except that it is created using a graphical interface.

Next, add a Get Position action in the controller variable you just stored, and set an object to gain the position of the controller in the scene. This will become your right hand controller.

Store this position value in a vector variable, and call it “controllerPosition”. We will use this variable later.

Next, we will gain the current rotation value of the controller, store it in a variable, and call it controllerRotation. To store the x, y, and z angle to a certain vector, we will get rotation and store the new variable as Euler Angles.

Find the RainbowCrossbow asset in the asset list. It will be located in FPL_ISBIT, WeaponsPack > 3D.

Drag the RainbowCrossbow into the Hierarchy. By doing so, we are now able to edit it in the Unity Scene Editor and transform it into a prefab. Set the scale of the Rainbow Crossbow to 0.2, 0.2, 0.2 so that the size will be good in the game.

Create a new Sphere as a child object of Rainbow Crossbow.

Move this sphere to the front of the crossbow, and disable Mesh Renderer by un-checking the box in the inspector window. The sphere will become the bullet spawn point, but it will be invisible from the player. (The player doesn’t need to see it)

Drag and drop the RainbowCrossbow object to the myPrefabs folder, and change it into a single prefab. Now, we have made the first custom prefab for this game! By using Unity, we can easily create and custom the whole object and transform it into a prefab.

One benefit of playMaker is that when you move a prefab back to the scene, all of the prefab instances have the same property and FSM code. Also, you can edit the prefab FSM code directly without moving it to the Scene Hierarchy.

Lets rename the sphere to spawnPointSphere. It will be easier to search for.

Once you complete the previous steps, since we already created a Prefab from it, we can now delete the RainbowCrossbow instance in the Hierarchy.

Next, we will connect the RainbowCrossbow Prefab to the controller, and start creating the gun. With the right hand gun controller selected, move to the FSM, and add a Create Object action. Drag the RainbowCrossbow Prefab we just created into the Game Object field. Set it’s position and rotation to match the controller’s, so they will move simultaneously. For this setting, use the controllerPosition and controllerRotation variable from previous steps. Then, store this into a new variable called “crossbow”. We will refer to this in later steps.

By testing the crossbow and controller positions, I found out that the most natural position for a user to shoot the crossbow is when the crossbow is rotated for 40 degrees.

Just as in the image below, add a Rotation action in the action browser to set the angle.

Since we just want to run the rotation once, deselect the “Every Frame” checkbox.

Then, create a Set Parent action, and set the controller as the parent. The crossbow will be set as a child of the controller, and always move simultaneously with the controller.

Add a Get Child action and find “spawnPointSphere”, which is a child of the crossbow object. This is a sphere with the same properties as the one we edited with Unity Scene Editor when creating the RainbowCrossbow prefab. Store it in a new variable called “spawnPointSphere” for later uses.

Now, lets delete the controller model so it doesn’t show up and hide the crossbow model. Create another Get Child action, and get the child of the controller named “Model”. This object includes the 3D model for a VR controller. Store this into a variable named controllerModel.

Next, destroy controllerModel using the Destroy Object action.

The destroy process is needed since there is no editable controller prefab in the Scene Hierarchy. The controller is made by the game, so we search for it by name and set the action for it.

Next, we will code the other logics needed for the right hand gun controller. Create a new state called “Waiting For Trigger”, and draw a transition from FINISHED to the new state. Right click on Setup, choose Add Transition > FINISHED, and click on FINISHED and drag the mouse to the new state.

Add two global events from the event list named “Global_TriggerPressDown_R” and “Global_TriggerPressUp_R”. These event names must be the exact same spelling, and will be linked with STYLY when you upload this game.

Add “Global_TriggerPressDown_R” as a transition from the Waiting For Trigger state.

This trigger will be used when the user shoots a bullet. Add “Global_TriggerPressUp_R” as a global transition, just as in the image below.

The reason we added this as a global transition is because when the trigger is pressed down, a loop is created and bullets continue to emerge and get shot as long as the trigger is pressed, and we need a way to forcefully break the loop, and return to the Waiting For Trigger state without using any difficult logics. To be short, we want the bullets to stop emerging when the trigger is released.

Next, lets create a new state called “spawning bullet” with a transition from Waiting For Trigger through Global_TriggerPressDown_R.

The first action needed in the Spawning Bullet state is to get the rotation of spawnPointSphere, and set the bullet’s direction to the same angle. This is a must in order to find out the force direction of the emerging bullet. Add a GetRotation action in the action browser, and get the rotation of the spawnPointSphere as shown in the image below.

Then, we need a Create Object action to create the bullet. We will set the spawnPointSphere to find the position to create the bullet, and spawnPointRotation to find the rotation angle of the bullet. This angle is the same as the one we got from previous actions.

We haven’t created the bullet prefab yet, so we will leave the Game Object field blank for now.

PlayMaker notifies you with a red exclamation mark when there is a problem with an action. This will let you find this action easily afterwards.

When Spawning Bullet ends, we want to create another bullet, but we also want some time in between. To achieve this without complicating the Spawning Bullet state, create a waiting state with a wait action. As in the image below, set it so that when Spawning Bullet is finished, have it transition to a state called Waiting, which transitions back to Spawning Bullet when finished.

Set the Wait action in the Waiting state so that it waits for 0.2 seconds.

Set the Finish Event for Wait to FINISHED, and check Real Time. Now, 0.2 seconds in real time will be placed between the bullet shots.

That will be all for the gun controller settings. In the next article, I will introduce how to set up the bullets.

[Related Articles]

Using PlayMaker to create a game, and exporting it to STYLY -1-

Using PlayMaker to create a game, and exporting it to STYLY -3-

Using PlayMaker to create a game, and exporting it to STYLY -4-

Using PlayMaker to create a game, and exporting it to STYLY -5-

Using PlayMaker to create a game, and exporting it to STYLY -6-

Using PlayMaker to create a game, and exporting it to STYLY -7-