0 votes

enter code hereI have a problem (and my inglish is not my native language and so my bad grammar), i don't know how to make my character move and jump at the same time, i tried everything but i don't know where is my problem.

extends KinematicBody2D


export var GRAVITY = 500
export var Vel_H = 250
export var max_vel = 500
export var vel_Jump = 75
var Velocity = Vector2()
var animator_player
var Jump = false



func _ready():
    set_physics_process(true)
    Velocidad.x = 0
    Velocidad.y = 0
    animator_player = get_node("AnimationPlayer")



    func _physics_process(delta):

        ###########Gravity#############
        Velocity.y += GRAVITY * delta

        ###########procesa teclas############

        if(Input.is_action_pressed("mov_izq")):
            Velocity.x -= Vel_H
            if(!animator_player.is_playing()):
                animator_player.play("Mover")
            get_node("spr_Mario").set_flip_h(true)

        elif(Input.is_action_pressed("mov_der")):
            Velocity.x += Vel_H
            if(!animator_player.is_playing()):
                animator_player.play("Mover")
            get_node("spr_Mario").set_flip_h(false)

        else:
            Velocity.x = 0
            animator_player.stop()


            #######Jumpping###########
            if is_on_floor() and Input.is_action_pressed("salto"):
                Velocity.y = -vel_Jump 


        ##########Aproxima velocidad maxima###########
        Velocity.x = clamp(Velocity.x, -max_vel, max_vel)

        ##########Mueve###############
        var move = Velocity * delta

        ##########Ajuste de deslizamiento#############
        move_and_slide(move, Vector2(0, -1))

        if(is_on_floor()):
            Jump = false
in Engine by (15 points)

But if the character is not moving, the jump works perfectly.

Hi, I have a problem and I don't have idea how to solve it. Sometimes when the player is walking and I press space for jump, It doesn't jump.

1 Answer

+1 vote
Best answer

The "Jumping-Code" seems to be indented, as inside the "else" block. The else-block is only executed when none of the previous conditions are met, which in turn means you cant jump while one of the other conditions is true.
To fix it, just remove the tabbing before the jump-block.

by (46 points)
edited by

Thank you so much. I can't believe that the whole time I was fighting with the code, it was because of a tab that escaped me.

No problem :D It's often these simple mistakes that haunt you for hours.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.