Velocity shenanigans when jumping after landing on slope

Version 4.2

There is a alot to explain so i will try to do it quickly.
Essentially, in the game i am making, the player is able to run up slopes and walls like they are normal flat surfaces (kind of like Sonic the Hedgehog!). This is acheived mainly using these lines of code:

func _physics_process(delta):
	
	if is_on_floor():
		slopeangle = get_floor_normal().angle() + (PI/2)
		#angle of the floor rotated 90°
		
		slopefactor = get_floor_normal().x
		#affects acceleration based on slope steepness
	else:
		slopefactor = 0
		#slopes do not affect accelleration while in the air

	$Collision.rotation = rot
	
	$Sprite.rotation = lerp_angle($Sprite.rotation, rot, 0.25)

	if is_on_floor():

		up_direction = get_floor_normal()
		rot = slopeangle
	else:
		
		if not $Collision/Raycast.is_colliding() and grounded:
			grounded = false
			
			motion = get_real_velocity()
			
			rot = 0
			up_direction = Vector2(0, -1)

“$collision/Raycast” is just a small raycast pointing down towards the floor, placed at the bottom of the collisionshape2d.
There is also a peice of code which turns vertical momentum into horizontal momentum if the player lands on a slop of a certain angle:

	if is_on_floor():
		
		if not grounded:
			motion.x *= 0.8
			if abs(slopeangle) >= 0.25 and abs(motion.y) > abs(motion.x):
				motion.x += motion.y * slopefactor
				#adds vertical motion into horizontal motion when falling onto slope
			grounded = true

when the player jumps, they dont jump upwards, but instead in an up direction relative to their rotation:

		if abs(rot) > 1:
			position += Vector2(0,-(10)).rotated(rot)

in addition, theres a small line of code in the script that turns “Motion” into real velocity

	velocity = Vector2(motion.x, motion.y).rotated(rot)

With all of that being said, these things work fine on their own. However, in the specific circumstance that the player jumps at the moment they land on a slope, it launches them sideways at a speed which kind of breaks the movement system. I wish i could send a video to show what i mean, but im not allowed to send attachments :man_shrugging:

If you have any idea how i could fix this, i would love to know what it is. Thanks for your time, and take care.