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
asked Apr 3 in Engine by LlamasEmperor (15 points)

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.

answered Apr 3 by Penta (46 points)
edited Apr 4 by Penta

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.