Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | tuja0627 |
`extends “res://Characters/NPCs/PlayerDetection.gd”
onready var destinations = $“…/…/Destinations”.get_children()
onready var timer = $Timer
onready var navAgent : NavigationAgent2D
var target_pos: Vector2
var state = NAVIGATE
enum {
IDLE,
NAVIGATE
}
func _ready():
navAgent = $NavigationAgent2D
randomize()
decide_dest()
func _physics_process(delta):
match state:
NAVIGATE:
navigate()
IDLE:
idle_state()
func decide_dest():
target_pos = destinations[randi() % destinations.size() - 1].position
func navigate():
navAgent.set_target_location(target_pos)
var target_loc = navAgent.get_next_location()
look_at(target_loc)
var direction = position.direction_to(target_loc)
var velocity = navAgent.max_speed * direction
move_and_slide(velocity)
if navAgent.is_target_reached():
state = “IDLE”
func idle_state():
if timer.is_stopped():
timer.start()
func _on_Timer_timeout():
decide_dest()
state = NAVIGATE`