How do I fix flickering in corners from move_and_slide?

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

I’ve seen a lot of threads about this but no solutions for 2D. I’ve got move_and_slide on and when I walk into a tileset corner, my player starts rapidly switching between being on the ground and being in the air. How can I fix that?

Could we see some example code, or a screenshot of what is happening?

TheJokingJack | 2021-01-11 13:14

func _physics_process(delta):
var x_input = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")

if x_input != 0:
	$AnimationPlayer.play("run")
	motion.x += x_input * ACCELERATION * delta
	motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
	sprite.flip_h = x_input < 0
else:
	$AnimationPlayer.play("idle")

motion.y += GRAVITY * delta

if is_on_floor():
	if x_input == 0:
		motion.x = lerp(motion.x, 0, FRICTION)
	if Input.is_action_just_pressed("ui_jump"):
		motion.y = -JUMP_FORCE
else:
	$AnimationPlayer.play("jump")
	if Input.is_action_just_released("ui_jump") and motion.y < -JUMP_FORCE/2:
		motion.y = -JUMP_FORCE/2
	if x_input == 0:
		motion.x = lerp(motion.x, 0, AIR_RESISTANCE)
motion = move_and_slide(motion, Vector2.UP)

CherryInferno | 2021-01-11 22:11