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