Godot Version
Godot v4.4.1.stable.official [49a5bc7b6]
Question
I’m creating a game, but I want to know how do I make the player move on an isometric grid like what the game will have.
Godot v4.4.1.stable.official [49a5bc7b6]
I’m creating a game, but I want to know how do I make the player move on an isometric grid like what the game will have.
Are you using diamonds, hexagons, squares?
The basic approach, if you mean moving tile to tile, is to be able to map your grid position to a tile position. So if you are on grid position (2,2) and move to grid position (3,2), say, one tile to the right, you need to a function to convert that to a screen position. For squares you might use tile_height and tile_width. Diamonds and hexagons are a bit more complicated but once you have your mapping function the rest should be plain sailing.
There are lots of resources online for how to calculate your screen position from your isometric grid co-ordinates.
I hope that helps in some way.
How would I go about coding that function? I am using diamonds, by the way.
Something like:
func grid_to_isometric(x: int, y: int, tile_width: int, tile_height: int) -> Vector2:
var screen_x = (x - y) * tile_width * 0.5
var screen_y = (x + y) * tile_height * 0.5
return Vector2(screen_x, screen_y)
You can also use:
# for example
var tile_coordinate = Vector2(3, 4)
var tile_position = $TileMap.map_to_local(tile_coordinate)
There are tons of helpful methods in the tile map docs.
There are also some great tutorials online too.
I’ve tried it but my character still doesn’t move on the grid. Here’s my code:
extends CharacterBody2D
@export var speed = 100
func get_input():
var input_direction = Input.get_vector("left", "right", "up", "down")
velocity = input_direction * speed
func _physics_process(delta):
get_input()
move_and_slide()
func grid_to_isometric(x: int = 0, y: int = 0, tile_width: int = 32, tile_height: int = 16) -> Vector2:
var screen_x = (x - y) * tile_width * 0.5
var screen_y = (x + y) * tile_height * 0.5
return Vector2(screen_x, screen_y)
Oh dear. I think you might need to try a tutorial or two.
You have added a function but not used it anywhere. You are moving a body with move_and_slide not taking into account the grid at all. I don’t know where to start.
Do that tutorial, then you should be able to use the grid_to_isometric function to convert your grid position to a screen position for an isometric map.
Good luck, I hope it goes well.
Nevermind, I found my own solution to this, although the player cannot move diagonally yet.
extends CharacterBody2D
func _input(event):
if Input.is_action_just_pressed("up"):
position.y -= 16
if Input.is_action_just_pressed("down"):
position.y += 16
if Input.is_action_just_pressed("left"):
position.x -= 32
if Input.is_action_just_pressed("right"):
position.x += 32