参考
场景设置
extends Navigation2D
var start_point = Vector2()
var end_point = Vector2()
var path = []
func _input(event):
if event is InputEventMouseButton and event.pressed:
if event.button_index == BUTTON_LEFT:
print('button pressed left')
start_point = event.position
elif event.button_index == BUTTON_RIGHT:
print('button pressed right')
end_point = event.position
func _process(delta):
path = get_simple_path(start_point,end_point,false)
update()
func _draw():
for point in path:
draw_circle(point,10,Color(1,1,1))
draw_polyline(path,Color(1,0,0),3,true)
extends Navigation2D
var target_pos
var path = []
var speed = 500.0
var can_move = false
onready var player = $Sprite
func _input(event):
if event is InputEventMouseButton and event.pressed:
if event.button_index == BUTTON_LEFT:
target_pos = event.position
path = get_simple_path(player.position,target_pos,false)
update()
can_move = true
func _process(delta):
if can_move:
if path.size()>=2:
var now_pos = player.position
var dis = now_pos.distance_to(path[1])
if dis > 5:
player.position = now_pos.linear_interpolate(path[1],speed * delta/dis)
else:
player.position = path[1]
path.remove(0)
else:
can_move = false
func _draw():
for point in path:
draw_circle(point,10,Color(1,1,1))
draw_polyline(path,Color(1,0,0),3,true)
运行效果