Build an isometric 3D game in 2D — #3 Make blocks move

Fj max
7 min readApr 16, 2021

This is part 3 of the 2d/3d(game/engine) conversion used in my puzzle game: Ladder Box, which is available on Steam:

You can find all the textures, scripts in this GitHub repo:

Godot 3.2.x is used in this project, you can check the commits for what’s new in each part of this series.


Before we make more changes to movable and unmovable scripts:

name movable Movable and add every movable to group “movable”:

class_name Movable
extends BlockBase
func _ready():

name unmovable Unmovable:

class_name Unmovable
extends BlockBase
func _ready():

Store the grid content

We need to know what’s on our board and their position, and a place that stores this information, I’m putting this in the global Grid(

To make things more organized, let’s move something that can be calculated statically into another script.

Create a script call

class_name GridUtilsconst TEXTURE_SCALE = 3const texture_w := 24
const texture_h := 25
const SINGLE_X = Vector2(texture_w/2,texture_h/4) * TEXTURE_SCALE
const SINGLE_Z = Vector2(-texture_w/2,texture_h/4) * TEXTURE_SCALE
const SINGLE_Y = Vector2(0,-texture_h/2) * TEXTURE_SCALE
static func game_to_engine(x:int,y:int,z:int) -> Vector3:
var _rtn_2d = Vector2.ZERO
_rtn_2d += x*SINGLE_X
_rtn_2d += z*SINGLE_Z
_rtn_2d += y*SINGLE_Y
var _z = (x+y+z)*2
return Vector3(_rtn_2d.x,_rtn_2d.y,_z)
static func game_to_enginev(game_pos:Vector3) -> Vector3:
return game_to_engine(int(game_pos.x),int(game_pos.y),int(game_pos.z))
static func calc_xyz(v3:Vector3) -> int:
return int(v3.x+v3.y+v3.z)
static func game_direction_to_engine(direction:Vector3) -> Vector2:
match direction:
return -SINGLE_Z
return SINGLE_Z
return -SINGLE_X
return SINGLE_X
return SINGLE_Y
return -SINGLE_Y
return Vector2.ZERO

then after clean up and adding some function related to the grid content( which can not be accessed by static function):

# Godot Global/Autoload : Grid
extends Node
var game_arr = [] # a 3D array
# let's just assume we'll have a board with the size of V3(8,8,8)
var game_size = Vector3(8,8,8)
func _ready():
func set_grid_array():
for x in range(game_size.x):
for y in range(game_size.y):
for _z in range(game_size.z):
func get_game_axis(x,y,z) -> Object:
if !coordinate_within_range(x,y,z):
return Object()

return game_arr[x][y][z]
func get_game_axisv(pos:Vector3) -> Object:
return get_game_axis(pos.x,pos.y,pos.z)
func coordinate_within_range(x:int, y:int, z:int) -> bool:
if x <0 || y<0 || z<0 || \
x >= len(game_arr) || y >= len(game_arr[0]) || z >= len(game_arr[0][0]):
return false
return true
func coordinate_within_rangev(pos:Vector3) -> bool:
return coordinate_within_range(int(pos.x),int(pos.y),int(pos.z))
func set_axis_obj(obj:Object, x:int, y:int, z:int) -> void:
if coordinate_within_range(x,y,z):
game_arr[x][y][z] = obj
func set_axis_objv(obj:Object,pos:Vector3) -> void:
  • ‘game_arr’ is the variable that stores our grid content.
  • the board size is V3(8,8,8)

Change ‘set_game_pos’ in :

class_name BlockBase
extends Sprite
var game_pos:Vector3func _ready():
func set_game_pos(x:int,y:int,z:int):
game_pos = Vector3(x,y,z)
var engine_pos = GridUtils.game_to_engine(x,y,z)
self.position = Vector2(engine_pos.x,engine_pos.y)
self.z_index = engine_pos.z
func set_game_posv(new_pos:Vector3):

Finite State Machine

We will be using a finite state machine to make those blocks move the way we want. You can learn about the concept of FSM here.

First, we need to define the states for those movable blocks.

1. Idle

This is the initial state for our movables, and when they end their movement standing still.

2. Move

When we issue commands to make them move in four directions.

3. Jump

When blocks meet one block higher than their current game_pos.y, they will enter this state, and then they will continue moving in their direction, or fall to their previous position.

4. Fall

Fall to game-pos.y - 1.


That’s all the states for movable. In this part, we’ll get idle and move states work.

Code for FSM

Let’s first create scripts for state machine and state base class.

class_name StateBase
extends Node
onready var _stae_machine := get_parent()
var movable:Movable
# then we can minipulate our movable in our states
func _ready():
movable = owner as Movable
# we receive commands from our main scene
# in our senario, we will not handle use input in our states
func _command(_msg:Dictionary={}) -> void:
func _update(_delta:float) -> void:
# when entering the states,we run this function
func _enter(_msg:={}) -> void:
# run this when states exit
func _exit() -> void:

class_name StateMachine
extends Node
export var initial_state := NodePath()onready var state: StateBase = get_node(initial_state) setget set_state
onready var _state_name :=
var movable:Movablefunc _ready():
movable = owner as Movable
func _update(delta) -> void :
func receive_command(msg:Dictionary) -> void:
func switch_state(target_state_path:String,msg:Dictionary ={}) -> void:
if ! has_node(target_state_path):

var target_state := get_node(target_state_path)

self.state = target_state
func set_state(value: StateBase) -> void:
state = value
_state_name =

create nodes for states in the movable scene:

In the inspector, assign the initial state to ‘idle’.

and attach scripts to all those states that extend our StateBase class:

extends StateBase

Send command to movable

We’re gonna use ‘a,s,z,x’ to navigate our movable:

Let’s handle input in our main scene, then send command to movable to make them from idle to other states.

And add the function to handle input to send the command, in

func _input(event):
if event.is_action_pressed("movable_forward"):
elif event.is_action_pressed("movable_back"):
elif event.is_action_pressed("movable_left"):
elif event.is_action_pressed("movable_right"):
func send_command(command:Vector3) -> void:
for _m in get_tree().get_nodes_in_group("movable"):
  • also add ‘set_process_input(true)’ at _ready


extends StateBasefunc _command(_msg:Dictionary={}) -> void:
if _msg.keys().has("direction"):

Now we switch to the move state once we send the command in idle state, if we add something in _enter function of

extends StateBasevar direction: Vector3func _enter(_msg:Dictionary={}) -> void:
if !_msg.keys().has("direction"):

direction = _msg["direction"]
print("enter move")

And run the scene, you should see the output :)

At the _enter function of the, we will calculate how the movable move in the engine by the given direction, also the “next position” when the movable moved one block along that direction, I choose to make the calculation in a separate function called set_next_target, the “next” related variables will have a prefix “target”.

Now edit the

extends StateBasevar direction: Vector3
var engine_direction:Vector2
var target_game_pos:Vector3
var target_z:int
var target_engine_pos:Vector2
func _enter(_msg:Dictionary={}) -> void:
if !_msg.keys().has("direction"):

direction = _msg["direction"]
engine_direction = GridUtils.game_direction_to_engine(direction)
set_next_target()func set_next_target():
target_game_pos = movable.game_pos + direction
var _target_game_pos_obj = Grid.get_game_axisv(target_game_pos)

if Grid.coordinate_within_rangev(target_game_pos) && _target_game_pos_obj == null:
var _target_v3 = GridUtils.game_to_enginev(target_game_pos)
target_engine_pos = Vector2(_target_v3.x,_target_v3.y)
target_z = _target_v3.z
if direction == Vector3.FORWARD || direction == Vector3.LEFT:
movable.z_index = target_z + 1
movable.z_index = target_z - 1

Run the scene and hit z:

it moved.

But we don’t want them to move to the next block at once, we want them to move to their target gradually.


class_name BlockBase
extends Sprite
var game_pos:Vector3 = Vector3.INFfunc _ready():
# print(game_pos)
func initial_game_pos(x:int,y:int,z:int) -> void:
func initial_game_posv(new_game_pos:Vector3) -> void:
func engine_fit_game_pos():
var engine_pos = GridUtils.game_to_enginev(game_pos)
self.position = Vector2(engine_pos.x,engine_pos.y)
self.z_index = engine_pos.z
func set_game_pos(x:int,y:int,z:int) -> void:
if game_pos != Vector3.INF:
Grid.set_axis_obj(self, x, y, z)
game_pos = Vector3(x,y,z)
func set_game_posv(new_game_pos:Vector3) ->void:
  • separate the original set_game_pos into two functions

and change new_movable and new_unmovable in

func new_movable(x,y,z):
var _m = movable.instance()
func new_unmovable(x,y,z):
var _u = unmovable.instance()

change set_game_pos to initial_game_pos.

Then we need to make movable moves in _update function in the, but before we do that, we know the blocks move in a Vector 2 direction, and they are moving in a grid, but the distance of each update may vary, so after an update, the result of the movement may be:

  • haven’t reached target_engine_pos yet.
  • movable.position = target_engine_pos
  • movable has moved over the target_engine_pos.

So in the last two situations, both should be labeled movable has reached the target position, let’s create a script to help us do this:


class_name Math# start < target < end
static func is_between(start:float,end:float,target:float) -> bool:
var _start = round(start * 10)/10
var _end = round(end * 10)/10
var _target = round(target * 10)/10

var _tmp
if _start >= _end:
_tmp = _start
_start = _end
_end = _tmp

if _start <= _target && target <= _end:
if _end - _start >= _end - _target:
return true

return false
static func is_betweenv(start:Vector2,end:Vector2,target:Vector2) -> bool:
return is_between(start.x,end.x,target.x) &&\

Before we edit, we can define a constant in to define the move speed, I’m setting this to 2( this is slow but you can see the movement):

const MOVESPEED = 2

Then we can add the _update function in the

func _update(_delta:float) -> void:
var _after_move = movable.position + engine_direction * _delta * movable.MOVESPEED
var _reach_target = Math.is_betweenv(movable.position,_after_move,target_engine_pos)

if !_reach_target:
movable.position = _after_move
movable.position = target_engine_pos
movable.z_index = target_z

Run the scene:

The gif is slower than the running scene, but it works as expected.

That's it for this part, you can check out the Github repo for the code committed, to make it work as in Ladder Box, we still have a long way to go.

Stay tuned!