Sprite 2d dont folllow path2d

func move():
if currentpos + step >= 36:
currentpos += step
var targetpos = $NavigationAgent2D/Path2D.get_curve().get_point_position(36)
var tween = create_tween()
tween.tween_property(sprite_2d, “position”,targetpos , speed)
print (“currentpos”,currentpos,“step”,step)
checkwin()

else:
	currentpos += step
	var targetpos = $NavigationAgent2D/Path2D.get_curve().get_point_position(currentpos)
	print(targetpos)
	var tween = create_tween()
	tween.tween_property(sprite_2d, "position",targetpos , speed)
	print ("currentpos",currentpos,"step",step,tween)
	checkwin()

sprite_2d always go outside the windows area even tough it have the correct coordinate

Seems complicated, I believe you want to change the PathFollow2D’s progress property rather than the character’s position.

Why is there a NavigationAgent2D?

the code that i use as tutorial had that and i dont find tutorial using progress that tell me how to use it like i wanted to using undetermined range

The docs define progress as

The distance along the path, in pixels. Changing this value sets this node’s position to a point within the path.

play around with it in the inspector, slide the progress number left and right and watch it change the position.

As for code changes I think this is what it would look like; I’ve removed the duplicated code between if statements. I think you wanted the sprite to not go over the path length, but that can be achieved by disabling loop on the PathFollow2D

@onready var path_follow: PathFollow2D = $NavigationAgent2D/Path2D/PathFollow2D
func move() -> void:
    var targetpos = path_follow.progress + step
    var tween = create_tween()
    tween.tween_property(path_follow, "progress", targetpos, speed)
    checkwin()

I do not see how speed is defined but the tween’s fourth argument should be calculated as duration step / speed for consistent looking movement. Last thing is checkwin might want to wait for the tween to finish, you can use this line to do just that.

await tween.finished
checkwin()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.