Character Controller Question Ledges & Edges

How can I make it so the built in character controller doesn’t get stuck on ledges, so if they are too far off the object they will slide off. The Stepoffset does not work. I need the player to be forced off the edge/ slip off the edge in the picture provided and if they are on that ledge like that they may not be allowed to jump.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
    CharacterController characterController;
    PlayerInputActions actions;

    Vector2 currentMovementInput;
    Vector3 currentMovement;

    [SerializeField] float baseMoveSpeed = 3f;
    [SerializeField] float rotationFactorPerFrame = 10f;
    [SerializeField] float jumpHeight = 3f;
    [SerializeField] float gravity = -20f;

    bool isJumping;
    bool isMoving;

    private void Awake()
    {
        actions = new PlayerInputActions();
        characterController = GetComponent<CharacterController>();

        actions.Player.Move.performed += OnMovementInput;
        actions.Player.Move.canceled += OnMovementInput;
        actions.Player.Jump.started += OnJumpInput;
    }
    private void FixedUpdate()
    {
        HandleRotation();

        currentMovement.y += gravity * Time.deltaTime;

        characterController.Move(currentMovement * Time.deltaTime);

        if (characterController.isGrounded)
        {
            currentMovement.y = 0f;

            if (isJumping)
            {
                isJumping = false;
            }
        }
    }
    private void OnJumpInput(InputAction.CallbackContext context)
    {
        if (context.started && characterController.isGrounded)
        {
            float initialVerticalVelocity = Mathf.Sqrt(-2f * gravity * jumpHeight);
            currentMovement.y = initialVerticalVelocity;
            isJumping = true;
        }
    }

    private void HandleRotation()
    {
        if (isMoving)
        {
            Vector3 positionToLookAt = currentMovement;
            positionToLookAt.y = 0f;

            Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
        }
    }
    private void OnMovementInput(InputAction.CallbackContext context)
    {
        currentMovementInput = context.ReadValue<Vector2>();

        float isometricAngle = 45f;

        float isometricRad = isometricAngle * Mathf.Deg2Rad;
        float isometricCos = Mathf.Cos(isometricRad);
        float isometricSin = Mathf.Sin(isometricRad);
        float isoX = currentMovementInput.x * isometricCos + currentMovementInput.y * isometricSin;
        float isoZ = -currentMovementInput.x * isometricSin + currentMovementInput.y * isometricCos;

        currentMovement.x = isoX * baseMoveSpeed;
        currentMovement.z = isoZ * baseMoveSpeed;

        isMoving = currentMovementInput != Vector2.zero;
    }
    private void OnEnable()
    {
        actions.Player.Enable();
    }
    private void OnDisable()
    {
        actions.Player.Disable();
    }
}

FIGURED IT OUT FROM A VIDEO


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

[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
    public CharacterController characterController;
    PlayerInputActions actions;

    Vector2 currentMovementInput;
    Vector3 currentMovement;
    public Vector3 wallCheck = new Vector3(0.75f, -0.55f, 0f);

    [SerializeField] float raycastDistance = 0.05f;
    [SerializeField] float maxSlopeAngle = 45f;
    [SerializeField] float slipSpeed = 5f;
    [SerializeField] LayerMask groundLayer;

    [SerializeField] float baseMoveSpeed = 4f;
    [SerializeField] float rotationFactorPerFrame = 10f;
    [SerializeField] float jumpHeight = 2f;

    [SerializeField] float gravity = -20f;

    bool isJumping;
    bool isMoving;

    private void Awake()
    {
        actions = new PlayerInputActions();
        characterController = GetComponent<CharacterController>();

        actions.Player.Move.performed += OnMovementInput;
        actions.Player.Move.canceled += OnMovementInput;
        actions.Player.Jump.started += OnJumpInput;
    }
    private void FixedUpdate()
    {
        HandleRotation();

        currentMovement.y += gravity * Time.deltaTime;

        characterController.Move(currentMovement * Time.deltaTime);

        // Perform the ground check within FixedUpdate
        bool grounded = IsGrounded();
        if (grounded)
        {
            currentMovement.y = 0f;

            if (isJumping)
            {
                isJumping = false;
            }
        }
        else if (!grounded)
        {
            SlipDetection();
        }
    }

    private bool IsGrounded()
    {
        RaycastHit hitInfo;
        Vector3 raycastOrigin = transform.position - Vector3.up * characterController.height * 0.5f; // Start at the base of the player
        bool hit = Physics.Raycast(raycastOrigin, Vector3.down, out hitInfo, raycastDistance, groundLayer);
        return hit;
    }

    void SlipDetection()
    {
        RaycastHit hit;
        Vector3 raySpawnPosition = transform.position + Vector3.up * wallCheck.y;

        Ray frontRay = new Ray(raySpawnPosition, transform.forward);
        Ray backRay = new Ray(raySpawnPosition, -transform.forward);
        Ray leftRay = new Ray(raySpawnPosition, -transform.right);
        Ray rightRay = new Ray(raySpawnPosition, transform.right);

        // Additional diagonal rays
        Ray frontLeftRay = new Ray(raySpawnPosition, transform.forward - transform.right);
        Ray frontRightRay = new Ray(raySpawnPosition, transform.forward + transform.right);
        Ray backLeftRay = new Ray(raySpawnPosition, -transform.forward - transform.right);
        Ray backRightRay = new Ray(raySpawnPosition, -transform.forward + transform.right);

        float distance = wallCheck.x;

        bool slipDetected = false;

        if (Physics.Raycast(frontRay, out hit, distance, groundLayer) ||
            Physics.Raycast(backRay, out hit, distance, groundLayer) ||
            Physics.Raycast(leftRay, out hit, distance, groundLayer) ||
            Physics.Raycast(rightRay, out hit, distance, groundLayer) ||
            Physics.Raycast(frontLeftRay, out hit, distance, groundLayer) ||
            Physics.Raycast(frontRightRay, out hit, distance, groundLayer) ||
            Physics.Raycast(backLeftRay, out hit, distance, groundLayer) ||
            Physics.Raycast(backRightRay, out hit, distance, groundLayer))
        {
            if (Vector3.Angle(Vector3.up, hit.normal) > maxSlopeAngle)
            {
                HitForSlope(hit.normal);
                slipDetected = true;
            }
        }

        if (!slipDetected)
        {
            currentMovement.y += gravity * Time.deltaTime;
        }
    }

    void HitForSlope(Vector3 slipDirection)
    {
        if (!isMoving)
        {
            characterController.Move(((slipDirection * slipSpeed) + Vector3.down) * Time.deltaTime);
        }
    }

    private void OnJumpInput(InputAction.CallbackContext context)
    {
        if (context.started && IsGrounded())
        {
            float initialVerticalVelocity = Mathf.Sqrt(-2f * gravity * jumpHeight);
            currentMovement.y = initialVerticalVelocity;
            isJumping = true;
        }
    }

    private void HandleRotation()
    {
        if (isMoving)
        {
            Vector3 positionToLookAt = currentMovement;
            positionToLookAt.y = 0f;

            Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
        }
    }

    private void OnMovementInput(InputAction.CallbackContext context)
    {
        currentMovementInput = context.ReadValue<Vector2>();

        float isometricAngle = 45f;

        float isometricRad = isometricAngle * Mathf.Deg2Rad;
        float isometricCos = Mathf.Cos(isometricRad);
        float isometricSin = Mathf.Sin(isometricRad);
        float isoX = currentMovementInput.x * isometricCos + currentMovementInput.y * isometricSin;
        float isoZ = -currentMovementInput.x * isometricSin + currentMovementInput.y * isometricCos;

        currentMovement.x = isoX * baseMoveSpeed;
        currentMovement.z = isoZ * baseMoveSpeed;

        isMoving = currentMovementInput != Vector2.zero;
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.blue;

        Vector3 raySpawnPostion = transform.position + Vector3.up * wallCheck.y;

        Ray frontRay = new Ray(raySpawnPostion, transform.forward);
        Ray backRay = new Ray(raySpawnPostion, -transform.forward);
        Ray leftRay = new Ray(raySpawnPostion, -transform.right);
        Ray rightRay = new Ray(raySpawnPostion, transform.right);

        // Additional diagonal rays
        Ray frontLeftRay = new Ray(raySpawnPostion, transform.forward - transform.right);
        Ray frontRightRay = new Ray(raySpawnPostion, transform.forward + transform.right);
        Ray backLeftRay = new Ray(raySpawnPostion, -transform.forward - transform.right);
        Ray backRightRay = new Ray(raySpawnPostion, -transform.forward + transform.right);

        float distance = wallCheck.x;

        Gizmos.DrawRay(frontRay.origin, frontRay.direction * distance);
        Gizmos.DrawRay(backRay.origin, backRay.direction * distance);
        Gizmos.DrawRay(leftRay.origin, leftRay.direction * distance);
        Gizmos.DrawRay(rightRay.origin, rightRay.direction * distance);

        // Set a different color for the diagonal ray Gizmos
        Gizmos.color = Color.yellow;

        Gizmos.DrawRay(frontLeftRay.origin, frontLeftRay.direction * distance);
        Gizmos.DrawRay(frontRightRay.origin, frontRightRay.direction * distance);
        Gizmos.DrawRay(backLeftRay.origin, backLeftRay.direction * distance);
        Gizmos.DrawRay(backRightRay.origin, backRightRay.direction * distance);

        Gizmos.color = Color.red; // Use a different color for the ray cast gizmo

        Vector3 raycastOrigin = transform.position - Vector3.up * characterController.height * 0.5f; // Start at the base of the player
        Gizmos.DrawRay(raycastOrigin, Vector3.down * raycastDistance);
    }


    private void OnEnable()
    {
        actions.Player.Enable();
    }

    private void OnDisable()
    {
        actions.Player.Disable();
    }
}