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