How to move NPC with astar?

Godot Version



Hi, I’m fairly new to programming and Godot, not sure if I’m asking a stupid question, once I’ve astar3d implemented, and connected points, how exactly do I call NPCs to move on the path to reach the destination?? I’ve read through the documentation on Astar3d but don’t find anything on it. Do I need a Navigation Agent??

I’m not used to 3D, but I guess the mechanics is the same as 2D. With AStar you will create points connected to each other. With these points connected, now you have positions (Vector3 data) that you can provide to the NPC move function, and make it travel from one point to another. For example, the get_point_path method will return a PackedVector3Array, which is an array with all the positions the AStar implementation was able to find from the origin to the destination.

You don’t need Navigation Agent for that, since you have the points to provide a path. Maybe this tutorial can help you understanding how to use AStar:

1 Like

The NavigationAgents are for the navigation mesh based pathfinding of the NavigationServer.

The AStar classes are very simplistic point navigation helpers that are also completely detached from the rest of the navigation system.

So what supernova already mentioned, you add your points to astar in script, make the connections between the positions also in script, then you can query a path from astar that gives you the positions from your start to your target point if a path is possible. Then you use that path with your own movement code to move your actor from point to point until the target or last path point is reached.


Thanks. I watched the youtube but it doesn’t talk about how to code the movement. I tried tweening the NPC to each point with a for loop, but it doesn’t work very well. Any suggestion what I should use for movement?

Tweening is a good and simple solution to move an object around the connection points from the AStar implementation. Before trying to loop through the coordinates, you need to make sure the points that you want connected are connected using connect_points method. If they are not connected, the get_point_path might not return the expected path. With the AStar points added and connected in the scene, you can just use get_point_path to retrieve the Vector3 coordinates of each point in the path, loop through them and tweening between each point. Just remember to use await tween.finished signal to make the scene wait for the tween to end before moving for the next point.

This implementation here uses GridMap to visually build the locations in the scene and then uses AStar3D to add and connect points. Maybe it will be easier with a visual representation of each point.

1 Like