问题描述
只是学习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()))