Character controller sliding on edge

I’m using character controller for fps game but problem is when player touches any object he automatically starts sliding on edge. While player sliding on edge it’s impossible to move in any direction.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class FPSController : MonoBehaviour
{
    [Header("Settings")]
    [Range(0f, 10f)] public float walkSpeed = 1f;
    [Range(1f, 20f)] public float runSpeed = 2f;
    [Range(1f, 5f)] public float jumpPower = 1f;
  
    [SerializeField] private float gravity = 9.81f;
    [SerializeField] private CollisionFlags moveFlag;
    [SerializeField] private Vector3 moveDir;

    public PhysicMaterial groundprop;
    public bool isMoving = false;
    public bool isRunning = false;
    Vector3 lastPosition = Vector3.zero;

    [Header("Keys")]
    public KeyCode runKey = KeyCode.LeftShift;
    public KeyCode jumpKey = KeyCode.Space;

    CharacterController controller;
    float speed;

    public GameObject gameManager;
    WeaponManager weaponManager;
    Shooting2 shootingScript;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        weaponManager = gameManager.GetComponent<WeaponManager>();
        shootingScript = GetComponent<Shooting2>();
        speed = walkSpeed;
    }

    void Update()
    {
        float mSpeed = walkSpeed;
        if(Input.GetKey(runKey)) mSpeed = RunSpeed(); else speed = walkSpeed;

        float hMove = Input.GetAxisRaw("Horizontal") * mSpeed;
        float vMove = Input.GetAxisRaw("Vertical") * mSpeed;
      
        if(moveFlag == CollisionFlags.Below) moveDir = (hMove * transform.right) + (vMove * transform.forward);
        moveDir.y -= gravity * Time.deltaTime;

        if(Input.GetKeyDown(jumpKey) && controller.isGrounded) moveDir.y = jumpPower;

        moveFlag = controller.Move(moveDir * Time.deltaTime);

        //Footsteps
        isMoving = IsMoveing();
        lastPosition = transform.position;

        //Weapon Heands Animations
        if(isMoving && !shootingScript.isReloading && !shootingScript.isShooting && weaponManager.usingWeapon)
        {
            if(mSpeed == runSpeed) {
                isRunning = true;
                weaponManager.pistol.animator.SetBool("Walking", false);
                weaponManager.pistol.animator.SetBool("Running", true);
            }
            else {
                weaponManager.pistol.animator.SetBool("Running", false);
                weaponManager.pistol.animator.SetBool("Walking", true);
                isRunning = false;
            }
        }
        else
        {
            weaponManager.pistol.animator.SetBool("Walking", false);
            weaponManager.pistol.animator.SetBool("Running", false);
        }
    }

    public bool inAir()
    {
        if(controller.isGrounded) return false;
        else return true;
    }

    float RunSpeed()
    {
        speed += Time.deltaTime * 2f;
        speed = Mathf.Clamp(speed, 0f, runSpeed);
      
        return speed;
    }

    bool IsMoveing()
    {
        if(lastPosition == transform.position) return false;
        else return true;
    }

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        groundprop = hit.collider.material;
    }

I haven’t combined CC with Rigidbody… there are some considerations however, but google will guide you there.

One thing to try just off the top of my head is don’t execute line 54 when your movement is very small, like only execute it when moveDir.magnitude > 0.01f or so.

Thanks for the comment. I add ```
if(moveDir.magnitude > .01f) moveFlag = controller.Move(moveDir * Time.deltaTime);

but ofc I still have same problem.

is there any solution to this or is this unity bug ?