I created AStarGrid2D and made it so that the object moves on it, but the problem is that it moves in any direction except left, why is that?
code map
extends TileMap
func find_path(start: Vector2i, end: Vector2i) -> Array[Vector2i]:
var astar = AStarGrid2D.new()
astar.region = Rect2i(0, 0, 64, 64)
astar.cell_size = Vector2i(1, 1)
astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar.update()
for x in astar.region.size.x:
for y in astar.region.size.y:
var cell = Vector2i(x, y)
var tile_data = get_cell_tile_data(0, cell)
var cost = 1.0
if tile_data:
var custom_cost = tile_data.get_custom_data("movement_cost")
if custom_cost != null:
cost = custom_cost
else:
print("WARNING: the ", cell, " no movement_cost")
cost = 0.0 #
astar.set_point_solid(cell, cost <= 0.0)
var path: PackedVector2Array = astar.get_point_path(start, end)
var result: Array[Vector2i] = []
for point in path:
result.append(Vector2i(point))
return result
func _unhandled_input(event):
if event is InputEventMouseButton and event.pressed:
var target_cell = local_to_map(event.position)
var unit = get_node("BaseUnit")
if unit:
var unit_cell = local_to_map(unit.position)
var path = find_path(unit_cell, target_cell)
if path.size() > 1:
unit.set_path(path)
print("Путь найден: ", path)
var line = get_node_or_null("Line2D")
if line:
line.clear_points()
for cell in path:
line.add_point(map_to_local(cell) + Vector2(16, 16))
func _ready():
print("=== TILESET ===")
for x in 3:
for y in 3:
var cell = Vector2i(x, y)
var tile_data = get_cell_tile_data(0, cell)
if tile_data:
var cost = tile_data.get_custom_data("movement_cost")
print("Клетка ", cell, ": ", cost)
else:
print("Клетка ", cell, ": no date cell")
code characterBody2D
extends CharacterBody2D
@export var rotation_speed: float = 25.0
@export var move_speed: float = 80.0
var smech = Vector2(0,0)
enum State { IDLE, ROTATING, MOVING }
var current_state = State.IDLE
var current_path: Array[Vector2i] = []
var target_angle: float = 0.0
var target_cell: Vector2i = Vector2i.ZERO
func set_path(path: Array[Vector2i]):
if path.is_empty():
return
current_path = path
_start_movement_cycle()
func _start_movement_cycle():
if current_path.is_empty():
current_state = State.IDLE
return
target_cell = current_path[0]
current_path.remove_at(0)
var target_pos = get_parent().map_to_local(target_cell) + smech
target_angle = (target_pos - position).angle()
current_state = State.ROTATING
func _physics_process(delta):
match current_state:
State.ROTATING:
rotation = lerp_angle(rotation, target_angle, rotation_speed * delta)
if abs(rotation - target_angle) < 0.1:
rotation = target_angle
current_state = State.MOVING
State.MOVING:
var target_pos = get_parent().map_to_local(target_cell) + smech
var direction = (target_pos - position).normalized()
if direction == Vector2(0.0, 0.0):
print("direction = 0")
direction = Vector2(-1.0, 0.0)
velocity = direction * move_speed
print(direction , "direction")
print(position, "position")
print(velocity , "velocity")
move_and_slide()
if position.distance_to(target_pos) < 2.0:
print(" true")
position = target_pos
_start_movement_cycle()