Some weird PathFollow2D progression_ratio behaviour

Godot Version

4.2.2

Question

I’m facing a bug I can’t get around

Hi! I’m following the famous GDQuest RPG tutorial alongside a youtuber who has extended and updated it for Godot 4.

The bug it’s that when i click an unit and send it to move, it will move towards it and halfway trhought ‘teleport’ to the final destination.

Both tutorials are in 80x80 but I’m using 32x32 (I mention this bc it may be useful? at first some things were faulty and I had to repair them)

func _process(delta: float) → void:
_path_follow.progress += move_speed * delta
if _path_follow.progress_ratio >= 1.0:
_is_walking = false
# Setting this value to 0.0 causes a Zero Length Interval error
_path_follow.progress = 0.00001
position = grid.calculate_map_position(cell)
curve.clear_points()
emit_signal(“walk_finished”)

This is the code I think the bug is, bc progress_ratio reaches 1 before the unit has walked towards it.

Any ideas? Do you need a gif to show the bug? I’m really lost to where to start

There’s a preformatted text option in the bar above the text editor. Just press enter and then the preformatted text option, and paste your code where it says. It also helps us identify tabs and other stuff.

So, is it moving by the correct speed for the first half?

Hi! Thanks a lot for your attention, I will get the performatted text option and bring it here. Yes, the first half it moves at the correct speed (it even varies when I change it or unit to unit).

## Represents a unit on the game board.
## The board manages its position inside the game grid.
## The unit itself holds stats and a visual representation that moves smoothly in the game world.
@tool
class_name Unit
extends Path2D

## Emitted when the unit reached the end of a path along which it was walking.
signal walk_finished
@export var is_wait := false
## Shared resource of type Grid, used to calculate map coordinates.
@export var grid: Resource
## Distance to which the unit can walk in cells.
@export var move_range := 6
## The unit's move speed when it's moving along a path.
@export var move_speed := 600.0
## Texture representing the unit.
@export var skin: Texture:
	set(value):
		skin = value
		if not _sprite:
			# This will resume execution after this node's _ready()
			await ready
		_sprite.texture = value
## Offset to apply to the `skin` sprite in pixels.
@export var skin_offset := Vector2.ZERO:
	set(value):
		skin_offset = value
		if not _sprite:
			await ready
		_sprite.position = value

## Coordinates of the current cell the cursor moved to.
var cell := Vector2.ZERO:
	set(value):
		# When changing the cell's value, we don't want to allow coordinates outside
		#	the grid, so we clamp them
		cell = grid.grid_clamp(value)
## Toggles the "selected" animation on the unit.
var is_selected := false:
	set(value):
		is_selected = value
		if is_selected:
			_anim_player.play("selected")
		else:
			_anim_player.play("idle")

var _is_walking := false:
	set(value):
		_is_walking = value
		set_process(_is_walking)

@onready var _sprite: Sprite2D = $PathFollow2D/Sprite
@onready var _anim_player: AnimationPlayer = $AnimationPlayer
@onready var _path_follow: PathFollow2D = $PathFollow2D


func _ready() -> void:
	set_process(false)
	_path_follow.rotates = false

	cell = grid.calculate_grid_coordinates(position)
	position = grid.calculate_map_position(cell)

	# We create the curve resource here because creating it in the editor prevents us from
	# moving the unit.
	if not Engine.is_editor_hint():
		curve = Curve2D.new()


func _process(delta: float) -> void:
	_path_follow.progress += move_speed * delta
	if _path_follow.progress_ratio >= 1.0:
		_is_walking = false
		# Setting this value to 0.0 causes a Zero Length Interval error
		_path_follow.progress = 0.00001
		position = grid.calculate_map_position(cell)
		curve.clear_points()
		emit_signal("walk_finished")


## Starts walking along the `path`.
## `path` is an array of grid coordinates that the function converts to map coordinates.
func walk_along(path: PackedVector2Array) -> void:
	if path.is_empty():
		return
	print(path)
	curve.add_point(Vector2.ZERO)
	for point in path:
		##print(point)
		print(grid.calculate_map_position(point) - position)
		curve.add_point(grid.calculate_map_position(point) - position)
	cell = path[-1]
	_is_walking = true

Some simple bugs I was able to fix on my own but this completetly escapes my understadning or way to cahnge it. The unit stops walking when progress_ratio is > or equal to 1.0, 1 being it’s max number. Not sure why, it reaches that point way before the unit has reached his destination (which is correctly calculated bc the unit teletransport there).