# How do I add grid-based movement to a isometric tilemap?

Attention Topic was automatically imported from the old Question2Answer platform.

This is what I have already, but I want to try doing grid-based movement. Any ideas how to do this? This tilemap is not going to change, so I was thinking of having an array that has the center coordinates of every block in the tilemap. Right now I don’t have any code for the tilemap, but I did add some sample code to the player.

``````extends KinematicBody2D

const SPEED = 10000
var motion = Vector2()

# Converts any Vector2 coordinates or motion from the cartesian to the isometric system
func cartesian_to_isometric(cartesian):
return Vector2(cartesian.x - cartesian.y, (cartesian.x + cartesian.y) / 1.7)

# useful to convert mouse coordinates back to screen-space, and detect where the player wants to know.
# If we want to add mouse-based controls, we'll need this function
func isometric_to_cartesian(iso):
var cart_pos = Vector2()
cart_pos.x = (iso.x + iso.y * 2) / 2
cart_pos.y = - iso.x + cart_pos.x
return cart_pos

func _physics_process(delta):
# Everything works like you're used to in a top-down game
var direction = Vector2()

if Input.is_action_pressed("ui_up"):
direction += Vector2(0, -1)
elif Input.is_action_pressed("ui_down"):
direction += Vector2(0, 1)

if Input.is_action_pressed("ui_left"):
direction += Vector2(-1, 0)
elif Input.is_action_pressed("ui_right"):
direction += Vector2(1, 0)

motion = direction.normalized() * SPEED * delta
# Isometric movement is movement like you're used to, converted to the isometric system
motion = cartesian_to_isometric(motion)
move_and_slide(motion)

set_physics_process(true)
``````

As you were apparently able to find this tutorial how did you manage to not find this one? It’s the direct sequel, also from GDQuest, with code on github.

Must of missed it. Thanks a ton!

Colonial345 | 2020-03-30 17:04

this work for me, the numbers depends of your grid size

extends KinematicBody2D
var inputs = {
“ui_right”: Vector2.RIGHT,
“ui_left”: Vector2.LEFT,
“ui_up”: Vector2.UP,
“ui_down”: Vector2.DOWN
}

func _unhandled_input(_event):
for dir in inputs.keys():
if Input.is_action_pressed(“ui_right”):
position.x += 8
position.y += 4
if Input.is_action_pressed(“ui_left”):
position.x -= 8
position.y -=4
if Input.is_action_pressed(“ui_up”):
position.x += 8
position.y -= 4
if Input.is_action_pressed(“ui_down”):
position.x -= 8
position.y +=4