How to make a raycast, that does not cross walls

hello, I have a problem that my raycast go through the objects. I have an enemy, where he has a raycast that detects the player, but if there’s something in front of the player, the raycast goes through and keeps detecting it, but I don’t want that to happen.

Can someone help me?

Here is the script:

void Shoot()
{
    RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.left, RayCast_Distance, Player_Layer);

    if (hit.collider != null)
    {
        Timer += Time.deltaTime;
        CanShoot = Timer >= TimerShoot;
        if (CanShoot)
        {
            GameObject B = Instantiate(Bullet, FirePoint.position, Quaternion.identity);
            B.GetComponent<ShooterBullet>().speed = 4;
            Timer = 0;
            Destroy(B, 4f);
        }     
    }      
}

What is Player_Layer? It should be a LayerMask that you configure in the inspector.

The LayerMask should include the layers that you want the raycast to hit. If you want the raycast to hit whatever is in front of the player, the mask needs to include that too.

void Shoot()
{

RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.left, RayCast_Distance, ObjectsToExclude_Layer); // make a new variable and include the layer that you want to exclude and assign that in the inspector

if (hit.collider != null)
    return;

RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.left, RayCast_Distance, Player_Layer);

if (hit.collider == null)
    return;
    
Timer += Time.deltaTime;
CanShoot = Timer >= TimerShoot;

if (!CanShoot)
    return;
        
GameObject B = Instantiate(Bullet, FirePoint.position, Quaternion.identity);
B.GetComponent<ShooterBullet>().speed = 4;
Timer = 0;
Destroy(B, 4f);
          
}

I am not sure if this will work, I don’t have time to test this, so please let me know
I also removed unnecessary nestings.