Navigation2D not working correctly

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

Hey there,

Firstly I’d like to say that I’m still very new to this engine and I am experimenting with some of the 2D functionality. I found myself at a bit of a roadblock that had me scratching my head all night. Keep in mind my code is a bit messy as I have been, like I said, experimenting with GDScript.

Here is a clip of the issue: https://i.gyazo.com/f7c9d00bd1f806ab93b45e1b405a4390.mp4

As you can see the enemy AI gets completely while trying to navigate the generated path between itself and the player. My best guess at what is causing this is that I am generating a new path for every tick which is causing the enemy to get stuck. However, after further investigation, that doesn’t seem to be the case. Also, my tilemap is set up correctly so that is not something that needs to be considered either.

Below I have posted the pastebins relating to the enemy’s AI.
Enemy.gd: extends KinematicBody2Dvar motion: Vector2 = Vector2.ZEROvar speed = 110 - Pastebin.com
EnemySM.gd: # Extend the State Machine scriptextends "res://StateMachine.gd"# Ready fu - Pastebin.com
StateMachine.gd: extends Nodeclass_name StateMachinevar state = nullvar previous_state = - Pastebin.com

Thank you for your help in advance!

:bust_in_silhouette: Reply From: boruok

did you tried latest beta builds?

No, but I’m on version 3.4.4 stable build. Do the newest beta builds describe any changes to the Navigation2D functionality?

Halflove | 2022-04-09 03:03

dunno, i could read all 3 betas changelog list and search for navigation2d changes and make list functionality changes for you. But you could just download and try.

boruok | 2022-04-09 03:08

:bust_in_silhouette: Reply From: 1MochaChan1

I had encountered this problem once before, in my case I wasn’t using the points in the navigation array properly,

Assuming you’ve a similar setup where you create a list/array of points from the enemy node to you player node called path

what you need to do in your Enemy.gd script at line 31:

instead of equating the position of your character and first position in the path (path[0]):

if global_position == path[0]:

change it to something like this:

if global_position.distance_to(path[0]) < 1:

This checks if the distance between the Enemy node and the path[0] point is less than 1

From my intuition this weird thing happens would be because somehow the engine misses the point where global_position == path[0] and doesn’t remove the path[0] so your character just keeps chasing the path[0] which hasn’t been removed yet and gets stuck there.

Here’s a repo where I built my firest Godot game using these techniques you can have a look at it if you’d like:

Hey!

Thanks for your help, I tried that out and completely recoded everything and this is what the script looks like now: extends KinematicBody2Dvar velocity: Vector2 = Vector2.ZEROvar speed = 400 - Pastebin.com

Unfortunately, it still hasn’t solved the issue, can you spot anything that looks like it could be causing the problem?

Again, thank you very much for the help!

Halflove | 2022-04-09 15:12

can you check if the point is being removed from the array?
because other than that there shouldn’t be any reason for the node to get stuck in the same place.

Also if you haven’t done it already I would suggest that you play with the threshold value a little bit.

1MochaChan1 | 2022-04-10 13:56