微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用寻路导航 Godot

如何解决使用寻路导航 Godot

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

当我拖动鼠标时,会绘制一条寻路线,显示要遵循的路径。当我单击时,角色会跟随路径,这很好。

如何限制到 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。