Game Toolkit for Unity
Setting up the player’s movement controls.
In this tutorial we’ll do the basic player setup and create a schematic to control the player’s movement, i.e. running and jumping. The player control schematic will be extended in later tutorials to fire rockets and lay bombs.
The player’s basic setup involves adding object variables to store the player’s attributes and adding a simple schematic to set the player. Select the hero game object in the scene (it’s an already added instance of the hero prefab found at Assets/Tutorial Resources/Prefabs/) and add the following components.
First, we’ll add object variables to the player’s game object. Add an Object Variables component (e.g. using the component menu: Makinom > Scenes > Object Variables).
Change the following settings of the object variables component.
Click on Add Variable to add a new variable to the object variables. This variable will be used remember which direction the player is facing, since we’re building a 2D game, we wont rotate the game object, but flip the sprite by inverting the scale on the X-axis.
Again, click on Add Variable. This variable will be used to store the player’s health.
Now, we’ll use a schematic to set the player when the level starts and add it using an Auto Machine component.
Follow the steps in this schematic tutorial – it explains how to create the schematic and add an auto machine to the game object.
Next, we’ll create a schematic to handle the player’s control. For now, we’ll only set up running and jumping – firing rockets and laying bombs will be added to this schematic at a later time. We’ll be making use of node layers to separate the different control parts. The player will be moved by a Rigidbody2D component.
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
The following nodes are placed in layer 0: Base Layer. Layer 0 is the default layer.
We’ll add an audio clip resource to play a random jump sound effect.
Click on Add Audio Clip Resource to add an audio clip resource.
We need 3 audio clips, add the needed amount by clicking on Add Audio Clip.
Add Node > Value > Variable > Change Variables
We’ll use this node to store the player’s input into a Vector3 variable. The player can only move horizontally (left and right).
Click on Add Variable to add a variable change.
The X-axis will use the input from the Horizontal input key.
The Y and Z-axis will just be 0.
Add Node > Animation > Mecanim > Play Mecanim Animation
This node is used to play an animation or set parameters of an animator. We’ll use it to play the walking animation of the player by setting the Speed float parameter.
Click on Add Parameter to add an parameter that will be changed.
Add Node > Value > Vector > Vector3 Multiply
We’ll use this node to multiply the input vector by the movement force of our player.
Add Node > Game Object > Rigidbody > Rigidbody Add Force
We’ll use this node to add the movement force to the player.
We’ll use the input variable as force.
Add Node > Movement > Movement > Clamp Transform
We’ll use this node to limit the velocity of the player, otherwise he would just continue to get faster due to adding force to the rigidbody.
Add Node > Value > Variable > Check Variables
We’ll use this node to check if the player is moving to the right but not facing right – in that case we’ll need to flip the sprite.
Click on Add Variable to add a variable condition.
Click on Add Variable to add another variable condition.
We’ll use this node to check if the player is moving to the left but facing right – in that case we’ll need to flip the sprite.
Copy the previous Check Variables node and connect the new node to the Failed slot of the previous node.
In the first variable condition, change the following settings.
In the 2nd variable condition, change the following settings.
Add Node > Machine > Start Tagged Machine
We’ll use this node to start a tagged machine with the tag flip. This will flip the sprite for us – we’ll set up the schematic for this later.
This node is connected to the Success slots of both Check Variables nodes.
Click on Add Tag to add a starting tag.
Next, we’ll add a Layer Gate node to the reach the next layer. This node is connected to the previous node’s Next slot and the 2nd Check Variables node’s Failed slot (i.e. when we’re moving left and already facing left).
Right click on the Next slot and select Add Node > Gate to Layer > Add New Layer. This will create a new layer and add a layer gate to it.
To display the new layer, either double click on the on the node’s title or select it in the layer popup field in the upper right corner of the editor.
You can rename the layer by clicking on the Rename Layer icon right of the popup field – e.g. name the layer Jump. Clicking on the icon again will stop editing the layer’s name.
The following nodes are placed in layer 1: Jump. In this layer, we’ll handle the player’s jumping.
Add Node > Input > Input Key
We’ll use this node to check if the player pressed the Jump input key.
This node is connected to the Next slot of the Gate Exit from layer 0.
Add Node > Game Object > Raycast
We’ll use this node to check if the player is on the ground by sending a raycast downwards. We don’t need to store any hit points or found game objects – this is just used to check if the ground is there.
This node is connected to the Jump slot of the Input Key node.
The Start Position defines where the raycast will start.
The Target Position defines where the raycast will be sent to.
The Layer Mask defines which layers can be hit by the raycast.
We’ll use this node to play the player’s jump animation by setting the Jump trigger parameter.
This node is connected to the Success slot of the Raycast node.
Add Node > Audio > Audio > Play Sound At Point
We’ll use this node to play the jump sound at the current position of the player.
We’ll use this node to add the jumping force to the player.
Finally, we’ll add a Layer Gate node to the reach the next layer. This node is connected to the previous node’s Next slot and the Failed slots of the Input Key and Raycast nodes.
You can rename the layer by clicking on the Rename Layer icon right of the popup field – e.g. name the layer Shoot. Clicking on the icon again will stop editing the layer’s name.
This layer will handle firing rockets, we’ll continue here at a later time. For now, this schematic is finished – click on Save Schematic and save it as PlayerControl in Assets/Schematics/.
The schematic handling the player’s control will try to start each frame, but will only be able to when the player is in control. We’ll stop the player control when the player died through changing the In Control game state.
Add a tick machine component to the hero game object (e.g. using the component menu: Makinom > Machines > Tick Machine) and change the following settings.
Click on Add Condition, we’ll set up the game state condition here.
In the added Condition 0, we’ll set up our game state condition.
Click on Add Game State.
Next, we’ll set up a simple schematic to flip a sprite.
We’ll use this node to toggle the facingRight bool variable – toggling a bool will change true to false and false to true.
To toggle the a bool variable, we’ll use itself as value and Negate it.
Add Node > Movement > Movement > Change Scale
We’ll use this node to negate the X-axis scale – this will turn 1 to -1 and -1 to 1.
When working with 2D (sprites), flipping them is done by using negative scales.
The X-axis will be the negated current X-axis scale.
The Y-axis will be reset to the current Y-axis scale.
The Z-axis will be reset to the current Z-axis scale.
And that’s it for the schematic – click on Save Schematic and save it as Flip in Assets/Schematics/.
Finally, we’ll add the tagged machine handling flipping the sprite. Select the hero game object in the scene and add a tagged machine component to it (e.g. using the component menu: Makinom > Machines > Tagged Machine). Change the following settings.
Click on Add Starting Tag to add a tag that can start this machine.
And that’s it for now – apply the changes to the prefab by clicking on Overrides > Apply All (top of the inspector).
Don’t forget to save the scene.
Click on Play to test the game. You’ll be able to move the player around using the arrow keys and jump using the jump key.
The next tutorial will handle the camera and background.