Search code examples
godotgdscript

Godot Input.is_action_pressed not working


Im trying to make a basic endless runner, however my character wont jump, Code:

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)

The character moves right continuously like he's supposed to and gravity works fine. Even if i remove the condition: if Input.is_action_just_pressed("ui-up"): the jump mechanic works and the character floats. As well as this, I have removed velocity.x = SPEED to see if that was the problem and it wasn't. I've checked the input map to ensure that space and the up arrow key are binded to "ui-up" and they both are. No errors occur so the only thing i can think of is that the condition for some reason is never True. I'm really confused as to why this isn't working and would appreciate any help.


Solution

  • The likely source of your problem is the combination of _physics_process and is_action_just_pressed. _physics_process is not guaranteed to be called every frame and so it can easily miss the action.

    A better solution would be to catch the jump in _input, store the information about the jump in a script-level variable and then look at this variable in _physics_process.