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. Roguelikes are turn based, i.e. the player will only be able to move when it’s the player’s turn – this will be handled using game states. You can learn more in the game states documentation.
First, we’ll use a schematic to set the player when the level starts and add it using an Auto Machine component. Select the Player game object in the scene view.
Follow the steps in this schematic tutorial – it explains how to create the schematic and add an auto machine to the game object.
The player can only move one tile per turn, either vertically or horizontally. Before moving, we need to check if there’s something on that tile blocking our way (and try to chip through it if possible).
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
We’ll add an audio clip resource to play a random move sound effect.
Click on Add Audio Clip Resource to add an audio clip resource.
We need 2 audio clips, add the needed amount by clicking on Add Audio Clip.
Add Node > Input > Input Key
This node is used to check if input keys are used – the first used input in the list will execute the node connected to it’s slot. We’ll use it to check if the player pressed the Vertical or Horizontal keys.
Click on Add Input Key to add another input.
Add Node > Value > Variable > Change Variables
We’ll use this node to set the direction we’ll be moving into when moving horizontally. This node is connected to the Horizontal slot of the Input Key node.
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.
We’ll use this node to set the direction we’ll be moving into when moving vertically. This node is connected to the Vertical slot of the Input Key node.
The Y-axis will use the input from the Vertical input key.
The X and Z-axis will just be 0.
Add Node > Game Object > Raycast > Raycast
This node is used to send a raycast into the scene. We’ll use it to check if something is on the tile we’re moving to.
We will store the game object we’ve hit with the raycast.
This node is connected to the Next slots of the 2 previous Change Variables nodes.
We need to make sure that the player isn’t found by the raycast, so we’ll use Filter Game Objects.
Click on Add Excluded Object to add a game object that will be ignored by the raycast.
We want the game object that was hit to be used as Selected Data in order to use it in the following nodes.
Add Node > Machine > Start Tagged Machine
We’ll use this node to start a tagged machine with the tag wall on the game object that was hit by the raycast. This will reduce the health of walls and remove them from the game – we’ll set this up in later schematics. The damage the player does to the wall is stored in a global variable (see first steps).
This node is connected to the Success slot of the Raycast node – i.e. when we’re blocked by something.
Click on Add Tag to add a starting tag.
Add Node > Animation + Audio > Audio > Play Sound
This node is used to play audio clips. We’ll use it to play the movement sound with a random pitch.
This node is connected to the Failed slot of the Raycast node – i.e. when we’re moving.
Add Node > Movement > Movement > Change Position
This node is used to change or fade the position of a game object. We’ll use it to move the player (fading) on the next tile.
We’ll use this node to reduce the player’s food by 1. The food is stored in a global variable.
This node is connected to the Next slots of the Start Tagged Machine (hitting walls) and Change Position nodes – i.e. both moving and being blocked by something costs food.
We’ve reduced the player’s food – now we need to check if all food is gone, i.e. game over.
Checking for game over will be handled by another schematic – we’ll set it up later, so for now we only try to start it’s tagged machine.
Add Node > Game > Game > Change Game State
We’ll use this node to end the player’s move by inactivating the In Control game state.
Click on Add Game State to add a game state change.
That’s it for the schematic – 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 (and the game is running). This allows us to set up the turn based gameplay.
Add a tick machine component to the Player 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.
Copy the game state and change the used state.
And that’s it for now – apply the changes to the player’s 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 for 1 tile – after that, the player’s turn ends.
The next tutorial will handle walls, food and exit.
© 2015 Gaming is Love e.U.
Disclosure: This site may contain affiliate links, which means I may receive a commission if you click a link and purchase something that I have recommended. While clicking these links won’t cost you any money, they will help me fund my development projects while recommending great assets!