Hello, characterBody2D, it doesn't move to the left

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()  

It’s helpful with things like this to print everything while running.

As for why it’s misbehaving, I’m not quite clear on what it’s not doing that it should. You say “moves any direction but left”, but does that mean:

  • It turns to the left but doesn’t start moving? If so, maybe it’s never getting out of ROTATING state for some reason?
  • It won’t even start turning left? If so, are you sure your A* grid will ever tell it to? Is it where you think it is on the A* grid, and is the A* grid where you think it is in world coords?
  • It does the first (or second) thing you ask but nothing else, and you’re always testing directions in the same order? Beware of this one; it’s really easy to get into a mental loop where you always test things exactly the same way, and jump to false conclusions about what’s wrong.

There could be other problems as well; more information would help.

Sorry. I checked, it turns, starts moving but stops abruptly. BUT! However, if I tell it to go down and then left, everything will work.

The problem was in the turn code. It turns but does not give the movement status.Although I still don’t know how to solve the problem

“lerp” is “linear interpolate”, and takes three arguments; the first position, the second position, and what position [0.0 .. 1.0] you want between them.

rotation is in radians, and there are 2π radians in a full circle. Your rotation_speed is 25.0, which (since you’re multiplying by delta, and we’ll assume you’re updating at 60fps) is going to come out to around 0.4167. Every invocation of lerp_angle() you’re telling it to move rotation 41% of the way to the target angle.

This means as the angle approaches the target, the rotation speed decreases. This may or may not be what you want.

If you want linear rotation, you need something more like:

var rot_speed:    float = PI * 10 # 180 degrees in 1/10th of a second
var old_rotation: float
var rot_position: float

[...]

func _start_movement_cycle():
    [...]
    current_state = State.ROTATING
    old_rotation  = rotation
    rot_position  = 0.0

func _physics_process(delta: float):
    match current_state:
        State.ROTATING:
            rot_position = clampf(0,0, 1.0, rot_speed * delta)
            rotation = lerp_angle(old_rotation, target_angle, rot_position)
            if rot_position >= 1.0:
                 current_state = State.MOVING