Grid-locked Movement in Unity

Ryan Sweigart
Nerd For Tech
Published in
3 min readAug 26, 2021

Objective: Create a movement system that locks the player into a grid.

Let’s say we’d like to create a block-pushing game like Sokoban. In such a game, the player character is confined to moving up, down, left, or right one tile at a time.

We’ll need the following variables:

The _blockMask will be set to detect anything on the Block layer; this will include walls and the crates we have to push.

The _destination will will be assigned to the player’s initial position in the Start method.

In the Update method, we’ll check if the player is very close to the destination. We do this by calculating the distance to the _destination and checking if it is less than Mathf.Epsilon, which is a very tiny number (it’s the smallest value that a float can have that is not 0).

If the distance is not close to 0, we’ll move the player towards the destination, as shown in the else block below.

If the player is very close to the destination, we’ll execute the Check Direction region hidden above.

This is when we gather the player’s input. Each of the four directions execute the same basic code, passing the direction into the CheckDirection method (more on that in a bit). If that method returns true, we set the payer’s destination in that direction. If it returns false, something must be blocking the player and we’ll ignore their input for this frame.

In the CheckDirection method, we’ll pass-in the direction to check. We’ll cast a ray from the player in the direction we’re checking, to a distance of _detectionRadius, ignoring anything that is not on the Block layer.

If the ray hit an object on the Block layer, we’ll look for the Pushable tag on that object. If we don’t find it, we’ve hit a wall and will return false, disallowing player movement in that direction. If the object is tagged Pushable, we’ll call its Push method. We don’t want the player to be able to move into the Pushable’s space until the Pushable itself moves, so we’ll return false.

If the ray did not find anything on the Block layer, the space is empty and we’ll return true, allowing the player to move.

Tomorrow we’ll look at how to push the blocks!

--

--