0 votes

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 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
    if not $PlayerWalkSound.is_playing():

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

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

    isWalking = false
    motion.x = 0

motion = move_and_slide(motion, FLOOR)
Godot version 3.2.3 Stable Official
in Engine by (22 points)

1 Answer

0 votes
Best answer

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?

by (1,012 points)
selected by

Thank you so much! It works now!

And no there werent any extra parts of code.

;)  No problem!

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 Frequently asked questions and 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 [email protected] with your username.