38 lines
888 B
GDScript
38 lines
888 B
GDScript
class_name AutoPathFollow extends PathFollow3D
|
|
|
|
@export var speed: float = 1.0
|
|
@export var pause_at_ends: float = 1.0
|
|
|
|
@onready var _prev_position := global_position
|
|
|
|
var _velocity := Vector3.ZERO
|
|
|
|
var direction: float = 1.0
|
|
var pause_timer: float = 0.0
|
|
var velocity: Vector3:
|
|
get():
|
|
return _velocity
|
|
var paused: bool:
|
|
get():
|
|
return pause_timer > 0.0
|
|
set(value):
|
|
if value:
|
|
pause_timer = INF
|
|
else:
|
|
pause_timer = 0.0
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if pause_timer > 0.0:
|
|
pause_timer -= delta
|
|
else:
|
|
progress += direction*speed*delta
|
|
if !loop && (
|
|
(direction > 0.0 && progress_ratio >= 1.0) ||
|
|
(direction < 0.0 && progress_ratio <= 0.0)
|
|
):
|
|
progress_ratio = clamp(progress_ratio, 0.0, 1.0)
|
|
direction *= -1.0
|
|
pause_timer = pause_at_ends
|
|
_velocity = (global_position - _prev_position).normalized()*speed
|
|
_prev_position = global_position
|