2D 8-way isometric movement

Godot Version

4.3

Question

I’m very new to game dev, and I’ve searched all over and even asked a friend about this:

I can’t find any information about how to make an 8-way movement code using get_vector() work on an isometric map.

essentially I need the movement to account for the 2:1 ratio of the grid. I’ve having trouble finding anything to implement. GDquest has a pretty old tutorial on this subject but no matter what I do I can’t get it to work.

I think it may have something to do with the way that get_vector2() automatically normalized the intercardinal directions but I’m not sure.

if anyone has any insight I’d be most appreciative.

and sorry if this is the wrong spot for this.

here’s my code:

extends CharacterBody2D

@onready var anim: AnimationPlayer = $AnimationPlayer
@onready var animation_tree: AnimationTree = $AnimationTree

var speed = 3000
var direction = Vector2()
var playback : AnimationNodeStateMachinePlayback


func _ready() -> void:
	playback = animation_tree["parameters/playback"]

func isometric_to_cartesian(cartesian):
	return Vector2(cartesian.x - cartesian.y, (cartesian.x + cartesian.y) / sqrt(3))

func _physics_process(delta: float) -> void:
# setup direction of movement
	direction = Input.get_vector("walk_left", "walk_right", "walk_up", "walk_down")
	velocity = direction * speed * delta
	isometric_to_cartesian(direction * delta)
	move_and_slide()
	select_animation()
	update_animation_parameters()


func select_animation():
	if velocity == Vector2.ZERO:
		playback.travel("idle")
	else:
		playback.travel("walk")
	
func update_animation_parameters():
	if direction == Vector2.ZERO:
		return
	animation_tree["parameters/idle/blend_position"] = direction
	animation_tree["parameters/walk/blend_position"] = direction

If you say its 2:1 all you have to do is scale an axis by 2 once you use the get_vector.

I assume 2 is the x axis of X:Y

direction = Input.get_vector("walk_left", "walk_right", "walk_up", "walk_down")
# scale
direction.x *= 2
velocity = direction * speed * delta
1 Like

You also call this function but dont use the return, could this be your problem?

1 Like

I’m really new so sorry if this sounds stupid but what would “using the return” look like?

So I solved my problem by looking at this demo: Isometric Game Demo - Godot Asset Library

it didn’t solve my problem of getting the isometric movement to work with get_vector() but it used a different method. Its from an older build of godot but all the code still works in 4.3

I wanted to use get_vector() because I had already created all of my animations in an animation player and set them up for blending in an animation tree. and it seemed like if I got the movement working correctly the animations wouldn’t blend correctly into the inter cardinals; and if I got the animations right - the movement would be off.

using this code I had to remake all the animations and attach them to an animated sprite, and then use an array to get it to work, but I’d still like to find out if there is a way to make it work with the animation tree and get_vector().