---
title: 04 Player Controls
description: "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…"
url: "https://makinom.com/guide/tutorials/2d-roguelike/04-player-controls/"
updated: "2021-01-06"
category: 2D Roguelike
---

# 04 Player Controls

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](https://makinom.com/guide/documentation/features/game-states/).

# Setting Player Schematic

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](https://makinom.com/guide/tutorials/schematics/setting-the-player/) - it explains how to create the schematic and add an auto machine to the game object.

# Player Control: Schematic

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.

[![](https://makinom.com/wp-content/uploads/makinom2_game_tutorial_2d_roguelike_schematics_player_control.png)](https://makinom.com/wp-content/uploads/makinom2_game_tutorial_2d_roguelike_schematics_player_control.png)

## Settings

We'll add an audio clip resource to play a random move sound effect.

### Audio Clips

Click on *Add Audio Clip Resource* to add an audio clip resource.

- **Name**
   Set to *Move*.
- **Use Order**
   Select *Random*.

We need 2 audio clips, add the needed amount by clicking on *Add Audio Clip*.

- **Audio Clip**
   Select *scavengers_footstep1* and *scavengers_footstep2*.
   The audio clips can be found at *Assets/Tutorial Resources/Audio/*.

## Input Key

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

- **Input Key**
   Select *Horizontal*.

Click on *Add Input Key* to add another input.

- **Input Key**
   Select *Vertical*.

## Change Variables

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

- **Change Type**
   Select *Variable*.
- **Variable Key**
   Set to *direction*.
- **Variable Origin**
   Select *Local*.
- **Type**
   Select *Vector3*.
- **Operator**
   Select *Set*.
- **Normalize**
   *Enable* this setting.
- **Vector3 Type**
   Select *Value >**Set Axis*.

The X-axis will use the input from the *Horizontal* input key.

- **X-Axis**
   Select *Input >**Input Key Axis* and *Horizontal*.

The Y and Z-axis will just be 0.

- **Y-Axis, Z-Axis**
   Set to *0* (Value).

## Change Variables

*Add Node > Value > Variable > Change Variables*

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.

Click on *Add Variable* to add a variable change.

- **Change Type**
   Select *Variable*.
- **Variable Key**
   Set to *direction*.
- **Variable Origin**
   Select *Local*.
- **Type**
   Select *Vector3*.
- **Operator**
   Select *Set*.
- **Normalize**
   *Enable* this setting.
- **Vector3 Type**
   Select *Set Axis*.

The Y-axis will use the input from the *Vertical* input key.

- **Y-Axis**
   Select *Input > Input Key Axis* and *Vertical*.

The X and Z-axis will just be 0.

- **X-Axis, Z-Axis**
   Set to *0* (Value).

## Raycast

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

### Raycast Settings

- **Raycast Origin**
   Select *Game Object*.
- **Object**
   Select *Machine Object*.
- **Distance**
   Set to *1*.
- **Vector3 Type (Direction)**
   Select *Vector3 Variable*.
- **Variable Key (Direction)**
   Set to *direction*.
- **Variable Origin (Direction)**
   Select *Local*.
- **Layer Mask**
   Select *Layer Mask* and the *BlockingLayer* layer.
   To only select the *BlockingLayer* layer, select *Nothing* first.

We need to make sure that the player isn't found by the raycast, so we'll use *Filter Game Objects*.

- **Use Filter**
   *Enable* this setting.

Click on *Add Excluded Object* to add a game object that will be ignored by the raycast.

- **Object**
   Select*Machine Object*.

### Hit Game Object

We want the game object that was hit to be used as *Selected Data* in order to use it in the following nodes.

- **Use Hit Object**
   Select *Selected Data*.
- **Data Key**
   Set to *hitObject*.
- **Data Origin**
   Select *Local*.
- **Change Type**
   Select *Set*.

## Start Tagged Machine

*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](https://makinom.com/tutorial/2droguelike/01-first-steps-3/)).

This node is connected to the *Success* slot of the *Raycast* node - i.e. when we're blocked by something.

- **Share Local Variables**
   *Disable* this setting.
- **Wait**
   *Enable* this setting.

### Machine Object

- **Object**
   Select *Selected Data*.
- **Data Key**
   Set to *hitObject*.
- **Data Origin**
   Select *Local*.

### Starting Object

- **Object**
   Select *Machine Object*.

### Tag Settings

Click on *Add Tag* to add a starting tag.

- **Tag**
   Set to *wall*.

## Play Sound

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

### Play On

- **Object**
   Select *Machine Object*.

### Audio Settings

- **Audio Clip**
   Select *0: Move*.
- **Play One Shot**
   *Enable* this setting.
- **Volume**
   Set to *1*.
- **Set Pitch**
   *Enable* this setting.
- **Random Pitch**
   *Enable* this setting.
- **Pitch**
   Set to *0.95*.
- **Pitch 2**
   Set to *1.05*.

## Change Position

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

### Moving Object

- **Object**
   Select *Machine Object*.
- **Move Component**
   Select *Rigidbody 2D*.
- **Move Function**
   Select *Move Position*.

### Target Position

- **To Object**
   *Disable* this setting.

### Position

- **Vector3 Type**
   Select *Vector3 Variable*.
- **Variable Key**
   Set to *direction*.
- **Variable Origin**
   Select *Local*.
- **Local Space**
   *Enable* this setting.
   We'll use the direction we've set using the input keys as an offset to the current position of the player.

### Move Settings

- **Move**
   *Enable* this setting.
- **Wait**
   *Enable* this setting.
- **Apply Gravity**
   *Disable* this setting.
- **Move By Speed**
   *Disable* this setting.
- **Time (s)**
   Set to *0.1*.

## Change Variables

*Add Node > Value > Variable > Change Variables*

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.

Click on *Add Variable* to add a variable change.

- **Change Type**
   Select *Variable*.
- **Variable Key**
   Set to *food*.
- **Variable Origin**
   Select *Global*.
- **Type**
   Select *Int*.
- **Operator**
   Select *Sub*.
- **Float Value**
   Set to *1* (Value).

## Start Tagged Machine

*Add Node > Machine > Start Tagged Machine*

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.

- **Share Local Variables**
   *Disable* this setting.
- **Wait**
   *Disable* this setting.

### Machine Object

- **Object**
   Select *Machine Object*.

### Starting Object

- **Object**
   Select *Machine Object*.

### Tag Settings

Click on *Add Tag* to add a starting tag.

- **Tag**
   Set to *checkGameOver*.

## Change Game State

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

- **Game State**
   Select *In Control*.
- **Set State**
   Select *Inactive*.

That's it for the schematic - click on *Save Schematic* and save it as *PlayerControl* in *Assets/Schematics/*.

# Player Control: Tick Machine

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.

## Start Settings

- **Update**
   *Enable* this setting.

## Machine Execution Settings

- **Schematics Asset**
   Select *PlayerControl*.
- **Execution Type**
   Select *Single*.
- **Update Type**
   Select *Update*.

## Condition Settings

Click on *Add Condition*, we'll set up the game state condition here.

- **Auto Check**
   Select *None*.
   We don't need to automatically check this when enabling or first initializing the game object.
   The conditions will be checked upon each start of the machine anyway.

In the added *Condition 0*, we'll set up our game state condition.

- **Condition Type**
   Select *Game State*.
- **Needed**
   Select *All*.

Click on *Add Game State*.

- **Condition Type**
   Select *Game State*.
- **Game State**
   Select *In Control*.
- **Check State**
   Select *Active*.

Copy the game state and change the used state.

- **Game State**
   Select *Game Running*.

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.

# Testing

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](https://makinom.com/guide/tutorials/2d-roguelike/05-walls-food-and-exit/).
