KinematicBody2D flying off when moving toward edges

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

My character which is controlled with KinematicBody2D is flying off / gain velocity when moving towards the edges.

Here the code that I’m using for movement:

public override void _PhysicsProcess(float delta)
{
    base._PhysicsProcess(delta);
    velocity.y += gravityMagnitude * delta;
    Vector2 snap = Vector2.Down;
    velocity = MoveAndSlideWithSnap(velocity, snap, upDirection: Vector2.Up, stopOnSlope: true);
}

Here’s a GIF showing my problem:

I don’t want the character to fly off like that if possible.
I’ve already tried:

  1. Applying Physics Material to the platforms and changing friction.
  2. Changing the shape of the player collider.

Can someone help me?