【问题标题】:How to stop kinematicbody walks whem released movement key?如何在释放运动键时停止运动体行走?
【发布时间】:2021-04-12 18:08:01
【问题描述】:
extends KinematicBody
var speed=10

var mouse_sensitivity=0.5
var direction=Vector3.ZERO
onready var head=$Head
func _ready():
    if Input.is_action_pressed("ui_cancel"):
        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
    if event is InputEventMouseMotion:
        rotate_y(deg2rad(-event.relative.x*mouse_sensitivity))
        head.rotate_x(deg2rad(-event.relative.y*mouse_sensitivity))
        head.rotation.x=clamp(head.rotation.x,deg2rad(-90),deg2rad(90))
func _process(delta):
    Vector3.ZERO
    if Input.is_action_pressed("mf"):
        direction-=transform.basis.z
    elif Input.is_action_pressed("b"):
        direction+=transform.basis.z
    direction=direction.normalized()
    move_and_slide(direction*speed,Vector3.UP)

我不知道我做错了什么。 我的 kinematicBody 释放一个键后一直在移动。

【问题讨论】:

    标签: godot


    【解决方案1】:

    关注direction

    var direction=Vector3.ZERO
    
    # ...
    
    func _process(delta):
        Vector3.ZERO
        if Input.is_action_pressed("mf"):
            direction-=transform.basis.z
        elif Input.is_action_pressed("b"):
            direction+=transform.basis.z
        direction=direction.normalized()
        move_and_slide(direction*speed,Vector3.UP)
    

    它以ZERO 开头。然后按下其中一个已识别的输入,然后对其进行修改。输入之后不再是ZERO

    当然,_process 的后续执行不会进入您修改direction 的分支。但是,direction 保留了它的价值。

    无论输入如何,此行都会运行:

    move_and_slide(direction*speed,Vector3.UP)
    

    directionZERO 时它不会移动。但同样,在第一次输入之后direction 不再是ZERO


    我相信你的意思是把direction = Vector3.ZERO写成_process的第一行。

    我提醒你注意你的警告:

    • 您没有使用delta。可能意味着代码无法补偿帧速率的变化。
    • 你是一个独立的表达式 (Vector3.ZERO)。那没有任何作用。你是不是想在那里做点什么?
    • 您正在丢弃move_and_slide 的返回值。 说实话,我通常会告诉 Godot 忽略这个。

    让您的脚本没有警告是一个好兆头。审查他们。如果你认为没有错,你可以告诉 Godot 忽略它们(这可以通过评论 # warning-ignore:... 来完成。参见 GDScript warning system)。告诉 Godot 忽略警告会在弹出新警告时更容易注意到。


    别的,我相信这段代码应该在_physics_process。这是来自move_and_slide documentation

    此方法应在 Node._physics_process 中使用(或在 Node._physics_process 调用的方法中),因为它在计算中自动使用物理步骤的 delta 值。否则,模拟将以错误的速度运行。

    【讨论】:

    • 谢谢,我认为 _process 与 _physics_process 相同,我尝试删除 _physics 但它不适用于我。
    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多