在 Godot 中, get_simple_path() 返回的路径似乎被某些东西抵消了

In Godot the path returned by get_simple_path() seems offset by something

刚刚学习 Godot,所以可能遗漏了一些明显的东西 我试图让玩家导航到在地图上单击的某个点。 路径是用某种我无法弄清楚的偏移量计算的。

感谢任何指点!

这里的问题复制非常少 https://github.com/kender99/Godot_path_finding_problem_demo

图中白点是鼠标点击,红色是生成的路径

可能有问题的代码是:

extends Node2D

var path : = PoolVector2Array()

func _unhandled_input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed:
            path = $Navigation2D.get_simple_path($Player.position, event.position)
            $Player.path = path         
            $Line2D.points = path
            print(path.size(), ' Path:',path, '  Player:', $Player.position, '  Target:', event.position)           
            update()  # so line and circles get drawn

func _draw():
    for p in path:
                draw_circle(p, 5, Color(200, 200, 200))
    

event.position是当前受访者的本地位置,你应该将事件位置转换为Navigation2D的本地位置,像这样:

path = $Navigation2D.get_simple_path($Player.position, $Navigation2D.to_local(event.position))

或使用这样的get_global_mouse_position()方法:

path = $Navigation2D.get_simple_path($Player.position, $Navigation2D.to_local(get_global_mouse_position()))