How to combine jump AND right/left ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mister_why
:warning: Old Version Published before Godot 3 was released.

Hi !

I’m trying to combine “jump and right” in my code, but itsn’t working at all :confused:

How to ? Thx :wink:

extends KinematicBody2D

const GRAVITY = 200.0
const WALK_SPEED = 10

var velocity = Vector2(0,0)

func _ready():
set_fixed_process(true)

func _fixed_process(delta):
velocity.y += delta * GRAVITY
if (Input.is_action_pressed(“ui_left”)):
velocity += Vector2(-WALK_SPEED,0)
elif (Input.is_action_pressed(“ui_right”)):
velocity += Vector2(WALK_SPEED,0)
elif Input.is_action_pressed(“ui_select”) and Input.is_action_pressed(“ui_right”):
velocity += Vector2(WALK_SPEED,-WALK_SPEED)
elif Input.is_action_pressed(“ui_select”) and Input.is_action_pressed(“ui_left”):
velocity += Vector2(-WALK_SPEED,-WALK_SPEED)
elif Input.is_action_pressed(“ui_select”):
velocity += Vector2(0,-WALK_SPEED)
else:
velocity.x = 0

var motion = velocity.normalized() * WALK_SPEED
motion = move(motion)
if (is_colliding()):
	var n = get_collision_normal()
	motion = n.slide(motion)
	velocity = n.slide(velocity)
	move(motion)
:bust_in_silhouette: Reply From: literalcitrus

Because all your input checks are in the same if-else block, as soon as one of them gets triggered, all following ones will be ignored, even if they resolve to true.

You’re checking to see whether Input.is_action_pressed("ui_right") is true before checking Input.is_action_pressed("ui_select") and Input.is_action_pressed("ui_right") so even if you are pressing right and jump, it will only run the code associated with moving right.

I suggest breaking up the logic in your if-else block to check for moving left and right individually from jumping, and only applying changes to your velocity on one axis at a time.

Thx, (I’m a noob with Godot), do I have to do that out of the “func fixedprocess(delta)” or inside but instead of a elif, I put an if statement ?

mister_why | 2018-01-24 23:12

Keep it inside fixed process, just change around some of your if statements so that a detection of the right button doesn’t prevent the jump button from being detected later on.

literalcitrus | 2018-01-24 23:14

I made an other “if” statement :

func _fixed_process(delta):
velocity.y += delta * GRAVITY
if (Input.is_action_pressed("ui_left")):
	velocity.x += -WALK_SPEED
elif (Input.is_action_pressed("ui_right")):
		velocity.x += WALK_SPEED
else:
	velocity.x = 0
	
velocity += velocity*delta
var motion = velocity * delta
motion = move(motion)

if (Input.is_action_pressed("ui_select")):
    velocity.y = - WALK_SPEED

if (is_colliding()):
	var n = get_collision_normal()
	motion = n.slide(motion)
	velocity = n.slide(velocity)
	move(motion)

More or less like I found in this script (#112) : Godot

But still not working, in this script everything works fine, but I want to understand why in my code it still not working.

Thx for your time

mister_why | 2018-01-24 23:52

I think the problem here is just the way you’ve ordered things. You’ve put your jumping code between your move() call and the is_colliding() check, so the y velocity you are applying for the jump is probably being overwritten by the velocity = n.slide(velocity) line.

Try this:

func _fixed_process(delta):
    velocity.y += delta * GRAVITY
    if (Input.is_action_pressed("ui_left")):
        velocity.x += -WALK_SPEED
    elif (Input.is_action_pressed("ui_right")):
        velocity.x += WALK_SPEED
    else:
        velocity.x = 0

    if (Input.is_action_pressed("ui_select")):
        velocity.y = - WALK_SPEED

    velocity += velocity*delta
    var motion = velocity * delta
    motion = move(motion)

    if (is_colliding()):
        var n = get_collision_normal()
        motion = n.slide(motion)
        velocity = n.slide(velocity)
        move(motion)

No major changes, just moved some things around.

literalcitrus | 2018-01-25 00:02

GG it works !

But if you look at the code in the Url, you’ll see that he didn’t put the jump code like you but like me.

Do you know why it works ?

mister_why | 2018-01-25 00:06

In that code, the velocity.y = -JUMP_SPEED line (line 115) comes after the velocity = n.slide(velocity) line (line 100) which means that the -JUMP_SPEED value can’t be overwritten before getting back to the initial move() call (line 68). In your original code you had sandwiched the jumping code between the slide and move calls which caused it to get overwritten by velocity = n.slide(velocity).

literalcitrus | 2018-01-25 01:04