How can I make NPCs wait for few seconds before navigating again?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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`

:bust_in_silhouette: Reply From: Wakatta

Timers are nice but since you use a STATE MACHINE like setup
delta time would be even better.

var idle_time = 0

func physics_process(delta):
    match state:
        IDLE:
            if idle_time > 0:
                idle_time -= delta
            else:
                idle_time = 0
                state = NAVIGATE
    ...........
    ...........
    if navAgent.istarget_reached():
        state = IDLE
        idle_time = 3.0 # 3 seconds