We have talked about game objects and components. Now it is time to have a look at Playmaker FSMs, states, and actions in them.
An FSM in Playmaker is a graph that consists of states and transitions between them, attached to a game object. It allows for a way of visual programming using different states of the graph and events that trigger transitions to other states.
If you select the Wall
prefab and open the playMaker panel, you will see that its FSM has two states: the default Start state where everything begins and another one called State 1 by default, with an arrow connecting the former to the latter. The arrow is a transition. You cannot remove the Start state or the one it is connected to, because if you could you would not need the FSM attached to the object. However, you can create new states and define transitions to them.
You can navigate the FSM view almost exactly the same way you would do the Scene view. Use the middle mouse button to drag the view. Use the left mouse button to move the states around. This does not change anything in the logic of the state machine, but lets you organize everything the way that makes sense to you, as well as look at the parts of the graph that you are most interested in at the moment if the whole state machine is too big to be shown at once.
A state in FSM is empty by default and does not do anything, a lot like an empty game object without any components attached to it. In order to make a state do something, you need to attach actions to it.
As we have seen in Chapter 2, Unity's and Playmaker's User Interface, you can add an FSM to an object by selecting the latter, right-clicking in the FSM view of the playMaker panel, and then selecting Add FSM from the contextual menu. You can remove an object from the Playmaker control by right-clicking the header of the Playmaker FSM (Script) component in Inspector and selecting Remove Component from the contextual menu. This will remove the FSM and erase all of the changes you made to it, including added states and transitions. An object has an FSM attached to it if there is a red Playmaker hieroglyph icon () next to its name in the Hierarchy panel.
As an example for this book, we will be making a version of the classic air hockey game. In order to begin, we will need to add a puck and a mallet. In this chapter, you will make the mallet move based on the mouse position and push the puck as you would expect it to do in real life.
Right now in your scene there are four walls, a background quad, a camera, and a directional light (created in Chapter 2, Unity's and Playmaker's User Interface). Now it is time to make things interactive. Let us start with a mallet.
Mallet
and make a dark green (RGB color set to 10
, 155
, 10
in the Color window) material called MalletMaterial for it, then assign the material to it, as you did for the Wall
prefab before.1.35
, 1.35
, 1.35
) and its position to (-6.5
, 1.45
, 0
).Character Controller
in the search bar, and then double-click on the Character Controller item in the list (as shown in the following screenshot). When Unity asks you if you want to replace the existing CapsuleCollider component, click on Replace.0
(it will be set to the minimum possible value, which is 0.0001
). We are doing this to make sure that our mallet's collisions look precise.Move
in the first text field from the top. It is responsible for the name of the currently selected state. When you enter the new name, you should see the state change in the FSM view as well.This action gets the cursor position in 3D space when you hover an object. Under the hood it draws an invisible ray (this action is called raycast) from the mouse position on the camera's near clipping plane (you can see it as one of the white gizmo rectangles in the Scene view when you select Main Camera). If there is something in the way of the ray, a ray hit gets detected, and Unity finds out where exactly it happened. In our case, we will use the background quad to get the position of the mouse cursor, and then make the mallet follow it.
Background
for clarity. Then find the Layer drop-down menu in the top-right corner of the Inspector panel, click on the drop-down button that says Default by default, and press Add Layer… in it. The appearance of Inspector should now change to reveal a list of tags and layers as shown in the following screenshot:This menu is called TagManager. Click on the right of User Layer 8 and enter Background
in the edit field that appears, then press Return on your keyboard. Select the Background object again and set its layer to Background by choosing the appropriate element from the drop-down Layer list you used to access the TagManager before.
1
. This determines how many layers you will set to interact with the raycast. Element 0 should appear below. In the drop-down list to its right, select the Background layer you created before.mousePos
in the New Variable field on the bottom. Click on the Add button. Set the Variable Type to Vector3. A Vector 3 variable contains three numbers: X, Y, and Z.It is important to note that the order of actions in the state matters: the actions mentioned earlier will be executed before the ones that are mentioned later, so if you want to use a variable set in the Mouse Pick action, you must make sure that Mouse Pick is above whatever action is going to use it (in our case it is Controller Simple Move).
18.188.77.32