My character can crouch jump right but not left, and I have no idea why...

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

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

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.

Solid_Turner | 2021-05-13 13:31

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.

timothybrentwood | 2021-05-13 13:35

:bust_in_silhouette: Reply From: Mrpaolosarino

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