Navigation agent 3D - enemy doesn't go back at the original position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Alexispower

Hi!

I am doing the following tutorial:

and I am stuck, I am at the part ( around 35 min ) to set our enemy to patrol between 2 place but when I am runing the scene, my enemy go from point A to B but doesn’t go back at his original emplacement

I tried to replace a few lines in the " timer function " to send him back at his original emplacement but he didn’t patrol either, he just go from point A to point B and go back point A but stuck there, I want him to Patrol between the 2 points, anyone can help ?

here my complete code:

I think the problems come from the last function , about the Timer, something wrong about the Waypoint and waypoint index

*** Please don’t take the indentation in consideration, I got an hard time to put everything correctly , 1st time I am doing it ***

  extends CharacterBody3D
    enum States{
    patrol,
    chasing,
    hunting,
    waiting
    }

var currentState: States
var navigationAgent: NavigationAgent3D
@export var waypoints: Array
var waypointIndex : int
@export var speed = 2
var patrolTimer : Timer

func _ready():
currentState = States.patrol
navigationAgent = $NavigationAgent3D
patrolTimer = $PatrolTimer
waypoints = get_tree().get_nodes_in_group("EnemyWaypoint")
navigationAgent.set_target_position(waypoints[0].global_position)
pass


func _process(delta):
match currentState:
States.patrol:
if(navigationAgent.is_navigation_finished()):
currentState = States.waiting
patrolTimer.start()
return


var targetPos = navigationAgent.get_next_path_position()
var direction = global_position.direction_to(targetPos)
velocity = direction * speed
move_and_slide()
pass


States.chasing:
pass

States.hunting:
pass

States.waiting:
pass


pass



func _on_patrol_timer_timeout():
currentState = States.patrol
waypointIndex += 1
if waypointIndex > waypoints.size() -1:
waypointIndex = 0
navigationAgent.set\_target\_position(waypoints\[waypointIndex\].global\_position)
pass 
:bust_in_silhouette: Reply From: crossbito

I don’t see any problems with your code, but I have suggestions that could potentially resolve the issue:

  • Ensure that the last line navigationAgent.set_target_location(waypoints[waypointIndex].global_position) is properly indented outside the if statement. It should not be part of the if block.

Example:

if waypointIndex > waypoints.size() - 1:
    waypointIndex = 0
navigationAgent.set_target_location(waypoints[waypointIndex].global_position)
  • Signal Connection: Verify that you have properly connected the signal. Based on your description, it seems like the enemy goes back at least once, indicating that the signal may be connected correctly. Double-check the signal connection to ensure it is correctly linked to the appropriate function or method.
  • Check if waypoints has at least two points

Everything was ok but your last point saved me… I didn’t set my 2nd waypoint, wow thanks for your help! I was looking into this since a few days!

thanks again

Alexispower | 2023-05-30 01:11