Godot Noob: Move and jump

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LlamasEmperor

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

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.

Manuel Lucero | 2020-08-22 19:38

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

Manuel Lucero | 2020-08-22 19:38

:bust_in_silhouette: Reply From: Penta

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.

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.

LlamasEmperor | 2020-04-04 01:43

No problem :smiley: It’s often these simple mistakes that haunt you for hours.

Penta | 2020-04-04 09:45