Godot Version
v4.2.2.stable.official [15073afe3]
Question
I have an array of 3D positions representing a path for a node to follow. This function should return the nearest point to the parent node (which follows the path). Once the parent node is close to a point, it increments the index and continues until it reaches the end of the array. My issue is that because the target position often changes, resetting the path, the function ends up returning the same point repeatedly.
Here’s the code i wrote:
##Returns the next position in global coordinates that can be moved to
func get_next_point() -> Vector3:
if target_position != null:
navPath = navNode.find_path(parent_position, target_position)
if not navPath.is_empty():
#print(navPathIndex)
if navPathIndex < navPath.size():
#if we are close enough, switch to the next point
var distance: float = abs(parent_position.distance_to(navPath[navPathIndex]))
if distance < target_desired_distance:
print("going to next point ",navPath[navPathIndex],", distance is: ", distance)
navPathIndex += 1
if navPathIndex < navPath.size():
return navPath[navPathIndex + 1]
else:
emit_signal("target_reached") # Signal that target is reached
print("Target reached!")
navPathIndex = 0 # Reset the index for next path
return Vector3.ZERO
else:
return navPath[navPathIndex + 1]
else:
emit_signal("target_reached")
print("Target reached!")
navPathIndex = 0
return Vector3.ZERO
else:
push_error("Failed to return a valid path")
return Vector3.ZERO
else:
push_warning("There is no target point set")
navPathIndex = 0
return Vector3.ZERO
I’m pretty confused on how to pull this off the logic seems ok for me, any help would be great