Hey all, I'm working on a platformer right now and I've come across a bug that's left me stumped. I'm using platforms with one way collision, so the player can jump through tiles and land on top of them. Most of the time this works as expected, but one in every 20 jumps onto a higher platform and you get bounced down right when the bottom of your character meets the top of the platform. It's difficult to describe, so I've included a video demonstrating the bug, as well as showing some normally behaving jumps: https://youtu.be/BWl8IS3hHik
I've noticed the bug only seems to happen when you are moving either left or right while jumping. Another thing I've noticed is that the player seems to move up about a pixel whenever you walk left or right, and drop back down a pixel when you stop moving (perhaps this is related to the bug?)
I've included the player's physics_process code below. Does anyone know what might be causing this? If any other information can help, please let me know.
func _physics_process(delta):
if alive:
var walk = 0
if can_move: #need to test if
if Input.is_action_pressed("ui_left"):
walk = -WALK_FORCE
if (!facing_left):
facing_left = true
$AnimatedSprite.flip_h = true
$Sword.position.x = -SWORD_DISTANCE
elif Input.is_action_pressed("ui_right"):
walk = WALK_FORCE
if (facing_left):
facing_left = false
$AnimatedSprite.flip_h = false
$Sword.position.x = SWORD_DISTANCE
if can_jump and Input.is_action_just_pressed("ui_jump"):
velocity.y = -JUMP_FORCE
can_jump = false
play_jump_sound()
jumping = true
my_gravity = gravity * JUMP_GRAVITY_MODIFIER
elif jumping:
if check_grounded() or !Input.is_action_pressed("ui_jump"):
jumping = false
my_gravity = gravity
if (walk == 0): #decelerate the player if they're not walking
velocity.x = move_toward(velocity.x, 0, STOP_FORCE * delta)
else:
velocity.x += walk * delta #accelerate the player in their desired direction
#bring the player towards max speed
if velocity.x > MAX_WALK_SPEED:
velocity.x = move_toward(velocity.x, MAX_WALK_SPEED, DECELERATE_FORCE * delta)
elif velocity.x < -MAX_WALK_SPEED:
velocity.x = move_toward(velocity.x, -MAX_WALK_SPEED, DECELERATE_FORCE* delta)
#velocity.x = (velocity.x, -WALK_MAX_SPEED, WALK_MAX_SPEED) #prevent player from exceeding max_walk_speed
apply_gravity(delta)
velocity = move_and_slide(velocity, Vector2(0, -1))
was_grounded = is_grounded
is_grounded = check_grounded()
if is_grounded != was_grounded:
emit_signal("grounded_updated", is_grounded)
if !was_grounded and is_grounded: #just landed, so play landing sound
playing_walking_sound = false
play_step_sound(-2)
elif is_on_floor() and walk != 0:
#play the initial step sound
if !is_walking:
is_walking = true
play_step_sound()
$StepTimer.start()
elif is_walking:
is_walking = false
if (is_on_floor() and !can_jump):
can_jump = true
elif (!is_on_floor() and $JumpTimer.is_stopped()) and can_jump:
$JumpTimer.start()