navigation2d not pathing around obstacles

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cleeverz


Working a bit more on my little twin stick prototype.

I have implemented a player that can shoot, and some bugs that chase the player.

I want to change the bugs’ movement from simply moving toward the player to be able to path around obstacles in the level.

To prepare for this, I started by using a line 2d to show me the pathing I get from each bug to the player using a navigation2D node.

I added a Navigation2D item to my main scene, and drew a navpoly inside of it’s navigationpolygoninstance that “cuts out” the obstacle on the map.

No in the code for the bug (edit: I mean in the_physics_process call for the bug) , I’m calling line.points = nav2D.get_simple_path(to_local(global_position), to_local(player.global_position))

I would expect that the line that gets drawn here would path around the obstacle, but they just draw straight through it.

What am I not getting here?

since i cannot see an error on the little code your showing us i will give you this link to an excellent tutorial.

Maybe it has something to do with your navigation setup!?

klaas | 2020-09-14 22:25

I actually used that tutorial to get as far as I did.

A few more details:

I’m using two instances that have their own move scripts, instead of a mouse clicking a given point like that tutorial.

The Navigation2D object and the player are children of the main scene.

A spawner script creates the bugs, but adds them as a child to the main scene.

There’s currently no other code that seems like it would be relevant to show for help. I made screenshots of the navpoly I drew and the resulting behavior, but there doesn’t seem to be a way for me to upload images from my machine on here, just from the web.

cleeverz | 2020-09-14 22:41

hmmm, okay.

Have you enabled “show navigation” in the debugging menu? Just to have a clear picture of what is there on runtime?

Have you set a breakpoint to analyse the current state of teh navigation object and its values?

klaas | 2020-09-14 22:48

Didn’t even know about “show navigation”. I assume you’re referring to the “Visible Navigation” option.

I’ll try that and see if it help!

cleeverz | 2020-09-14 23:24

Okay, so I tried show navigation.

The navigation area looks like I would expect it to, basically I have a big rectangle everything should be able to path through with a small rectangle in the middle that should need to be pathed around. I don’t see anything else helpful happening there.

I don’t know what I would look for in the navigation object, let me dig and fiddle on that.

cleeverz | 2020-09-14 23:36

Okay, it looks like it has something to do with parenting and/or what is and is not local scope/local data when the bug object calls out to nav2D.

I tried placing another Line2D object root node of the main scene, and added this code to it’s ready call:

$Line2D.points = $Navigation2D.get_simple_path(Vector2(25,25), Vector2(450,200))

Those coordinates produce a line that intersects the obstacle. The line paths around the obstacle just like I would expect.

cleeverz | 2020-09-15 00:11

I got it working.

Instead of having the Line2D be a child of the bug object and call out to the Navigation2D object from within the bug object, I changed the bug scene to create the Line2D objects then add them as a child to the main level scene:

line = line.width = 1 get_parent().add_child(line)

Then changed the call to Navigation2D to line.points = nav2D.get_simple_path(global_position, player.global_position)

Now it’s working as expected.

cleeverz | 2020-09-15 00:41