godot-start/script/demo08_path/my_player.gd

42 lines
827 B
GDScript

extends Sprite
var speed : = 400.0
var path : = PoolVector2Array() setget set_path
func _ready() -> void:
set_process(false)
func _process(delta: float) -> void:
var move_distance : = speed * delta
move_along_path(move_distance)
func move_along_path(distance : float) -> void:
var last_point : = position
for index in range(path.size()):
var distance_to_next = last_point.distance_to(path[0])
if distance <= distance_to_next:
position = last_point.linear_interpolate(path[0], distance / distance_to_next)
break
if path.size() == 1:
position = path[0]
set_process(false)
break
distance -= distance_to_next
last_point = path[0]
path.remove(0)
pass
func set_path(value : PoolVector2Array) -> void:
if value.size() == 0:
return
path = value
path.remove(0)
set_process(true)