0 votes

`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 physicsprocess(delta):
match state:
NAVIGATE:
navigate()
IDLE:
idle_state()

func decidedest():
target
pos = destinations[randi() % destinations.size() - 1].position

func navigate():
navAgent.settargetlocation(targetpos)
var target
loc = navAgent.getnextlocation()
lookat(targetloc)
var direction = position.directionto(targetloc)
var velocity = navAgent.maxspeed * direction
move
andslide(velocity)
if navAgent.is
target_reached():
state = "IDLE"

func idlestate():
if timer.is
stopped():
timer.start()

func onTimertimeout():
decide
dest()
state = NAVIGATE`

in Engine by (14 points)

1 Answer

+1 vote

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
by (6,876 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.