Search code examples
game-enginegame-physicsgame-developmentgodotgdscript

Godot - Player can't jump while walking


I'm new to godot, and I'm making a 2d platformer game.

I complete the player movement, but the player can't jump while he's walking.

I use gdscript btw.

Player.gd

extends KinematicBody2D

var motion = Vector2()

func _process(delta):
    motion = move_and_slide(motion, Vector2.UP)
    pass

func _physics_process(delta):
    if Input.is_action_pressed("ui_right"):
        motion.x = 120
        $Sprite.flip_h = false
        if is_on_floor():
            $AnimationPlayer.play("Move")
    elif Input.is_action_pressed("ui_left"):
        motion.x = -120
        $Sprite.flip_h = true
        if is_on_floor():
            $AnimationPlayer.play("Move")
    else:
        motion.x = 0
        if is_on_floor():
            $AnimationPlayer.play("Idle")

    if is_on_floor() == false:
        motion.y += 10
        if motion.y > 10:
            $AnimationPlayer.play("Fall")
        if motion.y < -10:
            $AnimationPlayer.play("Jump")

    if is_on_floor() && (Input.is_action_pressed("ui_up") || Input.is_action_pressed("ui_select")):
        motion.y = -200

    pass

Solution

  • try resetting your motion.y == 0 if you are grounded. I think this is happening because your motion.y is too high when you are pressing the button.

    and also try to use Input.is_action_just_pressed() for jumping. so the button will not apply force in every frame.