How to fix Jump movement, char only jumps and doesnt move right/left in mid air

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

Im new to programming as well as Godots GD Script.

The Jump movement works however, the character only jumps and doesnt move right/left in mid air. Also I cant Jump while im holding down right/left button.

This is the basic movement code for my character. The game is pretty simple, a platformer.

The Jump code is marked with **

extends KinematicBody2D 
const GRAVITY = 15
const ACCELERATION = 20
const MAX_SPEED = 30
const JUMP_FORCE = 150
const FLOOR = Vector2(0,-1)

var motion = Vector2()


func _physics_process(delta):

motion.y += GRAVITY

var walk = $PlayerWalkSound
var isWalking = false

if Input.is_action_pressed("ui_right"):
	isWalking = true
	motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
	$AnimatedSprite.flip_h = false
	$AnimatedSprite.play("Walk")
	if not $PlayerWalkSound.is_playing():
		$PlayerWalkSound.play()

elif Input.is_action_pressed("ui_left"):
	isWalking = true
	motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
	$AnimatedSprite.flip_h = true
	$AnimatedSprite.play("Walk")
	if not $PlayerWalkSound.is_playing():
		$PlayerWalkSound.play()

**elif Input.is_action_just_pressed("ui_up") and is_on_floor():
	motion.y = - JUMP_FORCE**

else:
	isWalking = false
	motion.x = 0
	walk.stop()
	$AnimatedSprite.play("Idle")

motion = move_and_slide(motion, FLOOR)
:bust_in_silhouette: Reply From: Yuminous

Your Jump doesn’t work when you hold down Left/Right because your code is saying is that: if you are pressing Right, else if you are pressing Left, then it won’t bother with Jump.

To fix this, make it a normal if statement like:

if Input.is_action_pressed("ui_right"):
   # Go Right if pressed
elif Input.is_action_pressed("ui_left"):
   # Else if Right isn't pressed, go Left if pressed
if Input.is_action_pressed("ui_up") and is_on_floor():
   # Jump if pressed (and on floor) regardless of any other inputs

But I can’t work out why your left/right movement wouldn’t work when airborne. There’s nothing that should be able to stop it apart from additional code.

Is there extra parts to your player movement?

Thank you so much! It works now!

And no there werent any extra parts of code.

Scyonix | 2021-07-12 12:04

:wink: No problem!

Yuminous | 2021-07-12 12:15