Godot Input.is_action_pressed 不工作

Godot Input.is_action_pressed not working

我正在尝试制作一个基本的无尽奔跑者,但是我的角色不会跳跃, 代码:

extends KinematicBody2D

#movement speed
const SPEED = 200
const GRAVITY = 10
const JUMP_POWER = -250
const FLOOR = Vector2(0, -1)

var velocity = Vector2()

func _physics_process(delta):
    velocity.x = SPEED

    if Input.is_action_just_pressed("ui-up"):
        velocity.y = -250
        print("jumped")
    velocity.y += GRAVITY

    velocity = move_and_slide(velocity, FLOOR)

角色像他应该的那样连续向右移动并且重力作用正常。即使我删除了条件:if Input.is_action_just_pressed("ui-up"): 跳跃机制起作用并且角色漂浮。除此之外,我还删除了 velocity.x = SPEED 以查看这是不是问题所在。我检查了输入映射以确保 space 和向上箭头键绑定到 "ui-up" 并且它们都是。没有错误发生,所以我唯一能想到的是,由于某种原因,条件永远不会为真。我真的很困惑为什么这不起作用,希望得到任何帮助。

问题的可能来源是 _physics_process 和 is_action_just_pressed 的组合。 _physics_process 不保证每帧都被调用,因此很容易错过动作。

更好的解决方案是在 _input 中捕获跳转,将有关跳转的信息存储在脚本级变量中,然后在 _physics_process.

中查看此变量