CharacterBody2D teleporting when another CharacterBody2D underneath changes direction using transform

Godot Version

v4.4-stable

Question

The project in question a 2D platformer, with a player character and an npc (enemy character), both as CharacterBody2D and with a CollisionShape2D equipped. The enemy character node also has a script making it always facing the player on the x axis.

The logic of the enemy character turning implemented a manipulation of the transform utility, applying the workaround given here due to the bug with scale, thus the entire character node including the collision box would also turn around when the character is changing direction.

func _physics_process(delta: float) -> void:
	var direction = global_position.direction_to(player.global_position).x
	if sign(direction):
		transform.x.x = sign(direction)
	
	move_and_slide()

The unexpected outcome is as the following video: (new users are restricted from uploading videos)

When the elf changes direction while the player is on it, the player would be teleported to another place, which is not an expected behavior. Any remedies for it?