Some upward movement with move_and_slide when you run into a wall

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

I’m having some issues with move_and_slide function in a platformer game. For some reason, if character is standing on floor and is running into a wall, there’s some upward motion that seems to come out of nowhere and is being applied every other frame.

Here’s how it looks (notice movement vector printed every frame in the console):

Here’s the code for movement:

extends KinematicBody2D

const SPEED = 50
const GRAVITY = 250
const JUMP_FORCE = -130
const JUMP_CUT = -80

#var can_attack = true
var motion = Vector2()
var last_frame_motion = Vector2()

func _physics_process(delta):
	update_motion(delta)
	
func _process(delta):
	update_animation(motion)	
	
func update_motion(delta):
	last_frame_motion = motion
	fall(delta)
	run()
	jump()
	jump_cut()
	motion = move_and_slide(motion, Vector2.UP)
	print(motion)

func update_animation(motion):
	$AnimatedSprite.update_anim(motion)
	
func fall(delta):
	motion.y += GRAVITY * delta
	motion.y = clamp(motion.y, -GRAVITY, +GRAVITY)

func jump():
	if Input.is_action_just_pressed("ui_up") and is_on_floor():
		motion.y = JUMP_FORCE
		
func jump_cut():
	if Input.is_action_just_released("ui_up") and motion.y < JUMP_CUT :
		motion.y = JUMP_CUT	
	
func run():
	if Input.is_action_pressed("ui_right") and not Input.is_action_pressed("ui_left"):
		motion.x = lerp(motion.x, SPEED, 0.1)
	elif Input.is_action_pressed("ui_left") and not Input.is_action_pressed("ui_right"):
		motion.x = lerp(motion.x, -SPEED, 0.1)	
	else:
		if last_frame_motion.y == 0:
			motion.x = lerp(motion.x, 0, 0.12)
			if motion.x > 0 and motion.x < 5:
				motion.x = 0			
			elif motion.x < 0 and motion.x > -5:
				motion.x = 0
		else:
			motion.x = lerp(motion.x, 0, 0.042)

I’ve even tried using move_and_slide_with_snap but had no success with it either.

Not sure what is the problem here

It’s quite an old thread, but I’m running into the same exact problem. If the OP is around, did you find what was causing this issue ?

I posted my problem on Reddit: Reddit - Dive into anything

da-dam | 2022-10-13 18:13

:bust_in_silhouette: Reply From: ejricketts

Most likely to do with the fact you are calling fall() every frame without checking if you are actually falling. Make an if statement to check if you’re falling within the fall function and it won’t always run.

This video is great for player movement in a nice clean way: