In my game, we have
var path_follow = path_2d.get_child(0)
var curve = path_2d.curve
# Check if Path2D has more than one node
if curve.get_point_count() > 1:
# Move the PathFollow2D along the path based on the creature's velocity
path_follow.offset += enemy['velocity'] * delta
# Update the creature's position to match the PathFollow2D position
enemy['pos'] = path_follow.global_position
To move a creature and:
func update_path2D(path_2d, new_path, enemy):
# Clear the existing points in the Curve2D
path_2d.curve.clear_points()
# Add the new path points to the Curve2D
for pos in new_path:
path_2d.curve.add_point(Vector2(pos[0], pos[1]))
# Reset the PathFollow2D offset
path_2d.get_child(0).offset = 0
# Store the path's baked length as a variable
enemy['path_follow_baked_length'] = path_2d.curve.get_baked_length()
To update the creature's Path2D according to a grid-based array of positions new_path like
new_path = [ [0,0], [0,1], [0,2], [1,3] ]
Note: we get new_path from running A*
I need a cheap way to check the previous/last node in new_path from the creature's path_2d variable.
The goal is to basically change the creature's direction (enemy['dir']) every time a new node is reached, without having to constantly check _process at every update (which is very expensive and lags the game).