Raycast goes through

I don’t know where is the problem, ray just goes through the wall. Layermask is checked correctly and origin is behind the wall. But it still goes through the cube. I want enemy AI not to shoot if there is a wall.
Code:

  public virtual bool SeeingAlligned()
        {
            if (seeingOrigin != null)
            {
                Ray ray = new Ray(seeingOrigin.position, seeingOrigin.forward*attackDistance);
                Debug.DrawRay(seeingOrigin.position, seeingOrigin.forward*attackDistance, Color.blue, 2f);
                return Physics.Raycast(ray, out hit, attackDistance, playerLayermask);
            }
            else
            {
                Debug.Log("No ray origin for line of sight check");
                return false;
            }
        }

The problem is in this line :

Physics.Raycast(ray, out hit, attackDistance, playerLayermask);.

In the second screenshot, we can see that you selected the “Default” layer for the wall.

You may want to change playerLayerMask to Physics.AllLayers or if you want to ignore the playerLayerMask, change it to ~playerLayerMask.

Check this link for more details.