Ok how about this.
After making your path with the mentioned
Path2D node, create a
navigation2D node and 2
NavigationPolygons as a child of that. They will act as walls to surround the
Path2D node, but don't let the path inside of the polygons. The covered parts are where the navigation avoids walking into. So if you start from inside the navigation polygons, then you should end up in the closest uncovered parts. In this case, the surrounded path. Just be sure to cover areas outside of your view too or else your navigation may walk outside of the screen.
That should be enough for node setup. Now to.. Oh wait! Almost forgot. We will also want a second Path2D node with a
PathFollow2D node as a child. Is will be needed when-
The code will be inside your player node/scene. We will need a reference to the Navigation 2D, the second Path2D, and it's PathFollow2D to start with.
Now after we get those important stuff, we will now create code to get a path. From your player's current position, to a point where the mouse has clicked. We will do that in a function when the mouse is clicked.
if event is InputEventMouseButton:
#nav is reference to Navigation2D node.
var path_points = nav.get_simple_path(position, event.position)
#we will now create a Curve2D resource with these points.
var curve = Curve2D.new()
for point in path_points:
#and now we put the curve into the Path2D node(second one).
path.curve = curve
path_follow.offset = 0
following_path = true
Now that the path is in the path node, we can now follow it to its end with the pathfollow2D node. Code for this will be run continuously; so we will put it in either
path_follow.offset += 50*delta #This number is the player's speed. Which you can change.
if path_follow.unit_offset >= 1:
path_follow.unit_offset = 1
following_path = false
global_transform = path_follow.global_transform
And that should be all! BTW I made this all up without Godot on me right now so if there is anything wrong, let me know.
I hope all this typing was not all in vain. I'm doing it on a phone!