I'm having trouble with my KinematicBody based character sliding on slopes. I've read some posts about this and most resources say using MoveAndSlideWithSnap with the stopOnSlope flag is supposed to prevent this, but I still find I'm sliding down rather shallow slopes of less than 15 degrees. Can anyone see what I'm doing wrong?

    void updateVelocity(float delta)
    {
        Vector2 moveDir = new Vector2((pressedLeft ? -1 : 0) + (pressedRight ? 1 : 0),
            (pressedUp ? -1 : 0) + (pressedDown ? 1 : 0));

        Vector3 zAxis = Transform.basis.Column2;

        velocity += gravityAccel * delta * down;

        if (moveDir.x != 0)
        {
            RotateY(-moveDir.x * turnSpeed * delta);
        }

        if (moveDir.y != 0)
        {
            Vector3 travelVel = velocity.Project(zAxis);
            if (travelVel.LengthSquared() < walkSpeed * walkSpeed)
            {
                velocity += -zAxis * moveDir.y * walkAccel;
            }
        }

        //Apply drag
        {
            float drag = IsOnFloor() ? dragGround : dragAir;

            Vector3 velPerpToGround = velocity.Project(down);
            Vector3 velPlelToGround = velocity - velPerpToGround;
            velocity += -velPlelToGround * drag;
        }

        floorNorm = down;
        if (IsOnFloor())
        {
            floorNorm = GetFloorNormal();
            stickToGround = true;
        }

        if (reqJump && IsOnFloor())
        {
            stickToGround = false;
            velocity += Vector3.Up * jumpVelocity;
        }
    }

    public override void _PhysicsProcess(float delta)
    {
        updateVelocity(delta);

        if (stickToGround)
        {
            velocity = MoveAndSlideWithSnap(velocity, -floorNorm, -down, true, 4, (float)(85 * Math.PI * 2 / 360));
        }
        else
        {
            velocity = MoveAndSlide(velocity, -down, true);
        }
    }

I'm not positive, but I think the issue may be with line 8, where you are applying gravity to move the player down. I think what you need to do to prevent sliding on slopes is only apply downwards gravity if the player is not colliding with the ground. I would try testing to see if only applying the downwards gravity if is_on_floor is false fixes the issue.

You have to set velocity.y to zero when is_on_floor() is true.

6 days later
a year later