I want to implement an enemy that patrols along a predefined path but my code isn’t working. I want to use move_and_slide so I think I can’t use the PathFollow2D node. What am I missing here?
class_name Enemy
extends CharacterBody2D
@export var path: Path2D
var speed := 10
var min_distance := 2
var current_target_index := 0
var baked_points: PackedVector2Array
func _ready() -> void:
baked_points = path.curve.get_baked_points()
func _physics_process(delta: float) -> void:
if global_position.distance_to(baked_points[current_target_index]) < min_distance:
current_target_index = wrapi(current_target_index + 1, 0, baked_points.size())
velocity = global_position.direction_to(baked_points[current_target_index]) * speed
move_and_slide()