使用寻路导航 Godot 限制移动

Limit movement using pathfinding navigation Godot

我想通过可用的移动次数来限制我的角色可以行走的距离

当我拖动鼠标时,会绘制一条寻路线,显示路径 followed.when 我单击,角色跟随路径,工作正常。

如何限制到 a 变量的距离。

到目前为止的代码

func _process(delta): # executes the pathfinding
    var direction = Vector3()
    var step_size = delta * SPEED
    if can_move:
        get_parent().get_node("Draw").visible = false
        curr_state = "moving"
        var destination = path[0]
        direction = destination - character.translation
        if step_size > direction.length():
            step_size = direction.length()
            path.remove(0)
        if path.size() <=0:
            can_move = false
            curr_state = "idle"
        character.translation += direction.normalized() * step_size
        print(character.translation.direction_to(destination))
        direction.y = 0
        if direction:
            var look_at_point = character.translation + direction.normalized()
            character.look_at(look_at_point, Vector3.UP)


func _unhandled_input(event): 
    if event is InputEventMouseMotion and curr_state == "idle":
        path = navi.get_simple_path(character.translation, mouse_catcher(event), true)
        draw_path(path, mouse_catcher(event))
        get_parent().get_node("Draw").visible = true
    if event.is_action_pressed("ui_confirm") and curr_state =="idle":
        can_move = true



func mouse_catcher(event):
    var from = $Camera.project_ray_origin(event.position)
    var to = from + $Camera.project_ray_normal(event.position) * 10000
    var target_point = get_parent().get_node("Navi").get_closest_point_to_segment(from, to)
    return target_point


func draw_path(path_array, target_point, visi = true):
    var im = get_parent().get_node("Draw")
    if visi:
        im.visible = true
    else: 
        im.visible = false
    im.set_material_override(m)
    im.clear()
    im.begin(Mesh.PRIMITIVE_POINTS, null)
    im.add_vertex(path_array[0])
    im.add_vertex(path_array[path_array.size() - 1])
    im.end()
    im.begin(Mesh.PRIMITIVE_LINE_STRIP, null)
    print(path_array)
    for x in path:
        im.add_vertex(x)
    im.end()

如果要限制角色移动

你有一个变量step_size,它保存了角色在框架上移动了多少。您可以累积它们以获得角色总共移动了多少:

character.translation += direction.normalized() * step_size
total_walked = total_walked + step_size

声明total_walked,初始值为0

您可以检查该总数是否会超过您希望角色移动的数量,如果是这种情况,请不要移动:

if total_walked + step_size < maximum:
    character.translation += direction.normalized() * step_size
    total_walked = total_walked + step_size

声明 maximum 一些对您的情况有意义的值。

而且,当然,当您想让角色再次移动时,您必须重置累计值 (total_walked = 0)(也许您也想更改 maximum)。


您可以限制也可以不限制最后一步,以确保角色恰好移动了最大值:

if total_walked + step_size >= maximum:
    step_size = maximum - total_walked

character.translation += direction.normalized() * step_size
total_walked = total_walked + step_size

如果要限制路径的长度

你得到的路径是一个点列表。您可以遍历它们并获得距离:

var total:float = 0.0
for index in path.size() - 1
    var a:Vector3 = path[index]
    var b:Vector3 = path[index + 1]
    var step:float = a.distance_to(b)
    total = total + step

我假设这些是 Vector3,因为你正在进行光线投射。

一旦超过最大值就截断:

var total:float = 0.0
for index in path.size() - 1
    var a:Vector3 = path[index]
    var b:Vector3 = path[index + 1]
    var step:float = a.distance_to(b)
    total = total + step
    if total > maximum:
        path.resize(index)
        break

您可能想也可能不想在末尾插入一个点以确保距离恰好是最大距离:

var total:float = 0.0
for index in path.size() - 1
    var a:Vector3 = path[index]
    var b:Vector3 = path[index + 1]
    var step:float = a.distance_to(b)
    if total + step >= maximum:
        path.resize(index)
        path.append(a + a.direction_to(b) * (maximum - total))
        break

    total = total + step