0 votes

If I crouch ( character stops moving ) and hold down the right key, the instant I press the jump button my character will uncrouch and jump right, but this won't happen when doing it towards the left. It's entirely independent of the direction the character us facing. Here's the code I use to control the character :

extends KinematicBody2D

export (int) var speed = 128
export (int) var jump_speed = -256
export (int) var gravity = 1024
export (float, 0, 1.0) var friction = 0.5
export (float, 0, 1.0) var acceleration = 0.5

onready var _animation_player = $AnimatedSprite

var velocity = Vector2.ZERO
var ducking = false
var pastVelocity

func _physics_process(delta):

    #movement

    var dir = 0

    if Input.is_action_pressed("move_left") and not ducking :
        dir -= 1

    if Input.is_action_pressed("move_right") and not ducking :
        dir += 1

    if Input.is_action_pressed("move_duck") and is_on_floor():
        ducking = true

    if Input.is_action_just_released("move_duck"):
        ducking = false

    if Input.is_action_just_pressed("move_jump") and is_on_floor():
        ducking = false
        velocity.y = jump_speed
        _animation_player.play("Jumping_Start")

    if dir != 0:
        velocity.x = lerp(velocity.x, dir * speed, acceleration)
    else:
        velocity.x = lerp(velocity.x, 0, friction)

    velocity.y += gravity * delta
    velocity = move_and_slide(velocity, Vector2(0, -1))

    pastVelocity = velocity


func _process(delta):
    #animations :

    if not is_on_floor() and velocity.x < 8 :
        _animation_player.play("Jumping_Mid")
        _animation_player.set_flip_h(true)
    elif not is_on_floor() and velocity.x > 8 :
        _animation_player.play("Jumping_Mid")
        _animation_player.set_flip_h(false)
    elif is_on_floor() and _animation_player.animation == "Jumping_Mid" :
        _animation_player.play("Jumping_End")
    elif velocity.x < -8 and is_on_floor():
        _animation_player.set_flip_h(true)
        if _animation_player.animation != "Running" :
            _animation_player.stop()
        _animation_player.play("Running")
    elif velocity.x > 8 and is_on_floor():
        _animation_player.set_flip_h(false)
        if _animation_player.animation != "Running" :
            _animation_player.stop()
        _animation_player.play("Running")
    elif velocity.x <= 8 and velocity.x >= -8 and is_on_floor() and not ducking :
        if _animation_player.animation != "Idle" :
            _animation_player.stop()
        _animation_player.play("Idle")
    elif velocity.x <= 8 and velocity.x >= -8 and is_on_floor() and ducking :
        if _animation_player.animation != "Ducking" :
            _animation_player.stop()
        _animation_player.play("Ducking")

    #HUD

    $Camera2D/HUD/DebugGUI/VBoxContainer/xVelocity.text = "xVelocity: " + str(velocity.x)
    $Camera2D/HUD/DebugGUI/VBoxContainer/yVelocity.text = "yVelocity: " + str(velocity.y)
    $Camera2D/HUD/DebugGUI/VBoxContainer/onGround.text = "onGround: " + str(is_on_floor())
    $Camera2D/HUD/DebugGUI/VBoxContainer/ducking.text = "ducking: " + str(ducking)

Any ideas ? I tried switching the left and right movement commands but no luck, I've been on this for the past couple hours and still have absolutely no clue, so help would be greatly appreciated !

Thanks in advance,
Hasmosis

Godot version 3.3 stable
in Engine by (12 points)

When you say, "this won't happen," do you mean that your character doesn't even jump? Or that they jump, but don't move to the left.

I would start by cleaning up your conditionals in your _process() function:

func _process(delta):

    if is_on_floor():
        handle_on_floor_animations()
    else:
        handle_air_animations()

func handle_on_floor_animations():
    if velocity.x < 8:
    ...

If you don't want to make separate functions it's one of the very few times I would recommend using nested ifs. I'm thinking your problem will pop out at you when you do this.

1 Answer

0 votes

in this part

if Input.is_action_pressed("move_left") and not ducking :
    dir -= 1

if Input.is_action_pressed("move_right") and not ducking :
    dir += 1

make the "move_right" 'elif'. Because if you don't it will counteract each other resulting in bugs.
I think that's the reason why it always jump right

by (378 points)
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.
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 webmaster@godotengine.org with your username.