Godot Version
4.3 Stable
Question
I’m trying to move a Sprite2D
on a path that I generate using AStarGrid2D
. I handle the movement inside _process
function but I’m having trouble giving the Sprite2D
a speed. I’ve tried player.position = grid_id_path[0] * speed * delta
but that doesn’t work and it gives me an error.
The scene tree is basically just a Node2D
with a TileMapLayer
as a child. Nothing else.
Here’s my code, please help me solve this. Thanks!!
extends TileMapLayer
var width = 64
var height = 32
var start_pos = Vector2i.ZERO
var target_pos = Vector2i.ZERO
var player: Sprite2D
var speed = 16
var astar_grid: AStarGrid2D
var grid_id_path # Local coords (1 = 16 global coord)
var grid_point_path # Global coords (steps every 16px / cell_size)
func _ready() -> void:
player = Sprite2D.new()
player.texture = preload("res://icon.svg")
player.scale = Vector2(0.125, 0.125)
player.position = Vector2(8.0, 8.0)
add_child(player)
astar_grid = AStarGrid2D.new()
astar_grid.region = Rect2i(0, 0, width, height)
astar_grid.cell_size = Vector2i(16, 16)
astar_grid.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
astar_grid.default_estimate_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar_grid.update()
for x in width:
for y in height:
set_cell(Vector2i(x, y), 0, Vector2i(1, 0), 0)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
target_pos = local_to_map(get_global_mouse_position())
print("TARGET: ", target_pos)
grid_id_path = astar_grid.get_id_path(
local_to_map(player.position),
target_pos
)
grid_point_path = astar_grid.get_point_path(
local_to_map(player.position),
target_pos
)
grid_id_path.remove_at(0)
for x in width:
for y in height:
set_cell(Vector2i(x, y), 0, Vector2i(1, 0), 0)
for point in grid_id_path:
set_cell(point, 0, Vector2i(24, 0), 0)
#
func _process(delta: float) -> void:
if target_pos and local_to_map(player.position) != target_pos:
player.position = map_to_local(grid_id_path[0])
grid_id_path.remove_at(0)