【问题标题】:Navigation2d use with move_and_slide in GodotNavigation2d 在 Godot 中与 move_and_slide 一起使用
【发布时间】:2019-06-10 12:56:13
【问题描述】:

为了清楚起见,重写和编辑。假设我有一个类似于以下示例的 2d 平台游戏:

https://github.com/godotengine/godot-demo-projects/blob/master/2d/kinematic_character/player.gd

现在...假设我有玩家位置 (vector2) 和敌人位置 (vector2)。敌人的移动就像上面例子中的玩家一样。我使用 get_simple_path 来构建从敌人位置到玩家位置的预先存在的点数组。如何使用带有 move_and_slide 的数组将敌人从 A 点带到 B 点?

【问题讨论】:

    标签: godot gdscript


    【解决方案1】:

    您可以尝试以下方法:

    const GRAVITY = 1000
    const SLOPE_SLIDE_STOP = false
    
    var speed = 250
    var velocity = Vector2()
    
    var points = [Vector2(100, 200), Vector2(200, 400), Vector2(400, 800)]
    var current_point = null
    
    func _physics_process(delta):
        if points and current_point is null:
            current_point = points.pop_front()
    
        if current_point:
            if current_point.distance_to(position) > 0:
                target_direction = (position - current_point).normalized()
    
                velocity.y += delta * GRAVITY
    
                velocity = lerp(velocity, target_speed, 0.1)
                velocity = move_and_slide(velocity, target_direction, SLOPE_SLIDE_STOP)
            else:
                current_point = null
    

    【讨论】:

    • 聪明,这基本上就是我现在正在做的事情,以检测玩家相对于敌人站在哪一边。我更多的是寻找一种循环遍历 Vector2 点数组的方法,但我刚刚意识到我应该能够自己解决这个问题。我会奖励你正确的答案。 :)
    • 谢谢!我更新了答案,使其适用于 Vector2 点列表。希望对您有所帮助。
    猜你喜欢
    • 2020-03-01
    • 1970-01-01
    • 2022-10-17
    • 2017-05-12
    • 2020-10-07
    • 1970-01-01
    • 2022-01-21
    • 2021-08-24
    • 1970-01-01
    相关资源
    最近更新 更多