When i go out of crouch in my 3D Unity Game under a wall i will go through or into that wall how can i stop it from going into the wall?

This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovment : MonoBehaviour
{

public CharacterController controller;

public float speed = 15f;
public float gravity = -9.81f;
public float jumpHight = 3f;
public float sprit = 24f;

[Header("Crouch")]
public float crouchYScale;
public float startYScale;
public float crouch = 7f;

public int maxJumpCount = 2;
public int jumpsRemaining = 0;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
public bool isCrouching = false;


private void Start()
{
    startYScale = transform.localScale.y;
}
// Update is called once per frame
void Update()
{
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
        jumpsRemaining = maxJumpCount;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);
    if (Input.GetKeyDown(KeyCode.LeftControl))
    {
        speed = crouch;
        isCrouching = true;
        transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
    }
    else if (Input.GetKeyUp(KeyCode.LeftControl))
    {
        speed = 25f;
        isCrouching = false;
        transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
    }
    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        speed = sprit;
    }
    else if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        speed = 25f;
    }
    if ((Input.GetButtonDown("Jump")) && (jumpsRemaining > 0))
    {
        velocity.y = Mathf.Sqrt(jumpHight * -2f * gravity);
        jumpsRemaining -= 1;
    }
    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);

}

}

A simple raycast should do the trick.
Shoot it from the head of the player in the direction of the body’s up axis, and adjust the distance to be the size of the player when he is up minus his size when he is crouched.
Just make sure that the LayerMask does not contains the player’s layer.