Make Node follow path2D in X amount of time.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By senkai

Hi there, right now I have a path and the child follows it correctly, however this is based on speed. Is there a way to make the node get from the starting point to the end point in X amount of seconds instead?

:bust_in_silhouette: Reply From: jgodfrey

Sure - the unit_offset property is especially useful here. Try something like this:

var total_time = 4
var running_time = 0

func _process(delta):
	running_time += delta
	if running_time >= total_time: running_time = 0
	$PathFollow2D.unit_offset = lerp(0, 1, running_time / total_time)

Set total_time to the time required to complete one lap of the Path2D.

Thanks a lot dude, this also helped me understand the path2D better.

senkai | 2022-11-08 12:34