How do i prevent the player to pick up objects behind a wall

So i have this script here. This is script shoots a raycast from the main camera, and picks up an object with layer “Interactable”. The issue is, my raycast doesnt stop when hitting a wall, and detects an object with the layer “Interactable”, which causes a lot of problems.

How do i fix that?

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

public class PlayerInteractions : MonoBehaviour
{
    [Header("InteractableInfo")]
    public float sphereCastRadius = 0.5f;
    public int interactableLayerIndex;
    private Vector3 raycastPos;
    public GameObject lookObject;
    private PhysicsObject physicsObject;
    private Camera mainCamera;

    [Header("Pickup")]
    [SerializeField] private Transform pickupParent;
    public GameObject currentlyPickedUpObject;
    private Rigidbody pickupRB;

    [Header("ObjectFollow")]
    [SerializeField] private float minSpeed = 0;
    [SerializeField] private float maxSpeed = 300f;
    [SerializeField] private float maxDistance = 10f;
    private float currentSpeed = 0f;
    private float currentDist = 0f;

    [Header("Rotation")]
    public float rotationSpeed = 100f;
    Quaternion lookRot;

    private void Start()
    {
        mainCamera = Camera.main;
    }

    //A simple visualization of the point we're following in the scene view
    private void OnDrawGizmos()
    {
        //Gizmos.color = Color.yellow;
        //Gizmos.DrawSphere(pickupParent.position, 0.5f);
    }

    //Interactable Object detections and distance check
    void Update()
    {
        raycastPos = mainCamera.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 0));
        RaycastHit hit;

        //if (Physics.SphereCast(raycastPos, sphereCastRadius, mainCamera.transform.forward, out hit, maxDistance, 1 << interactableLayerIndex))
            if (Physics.Raycast(mainCamera.transform.position, mainCamera.transform.forward, out hit, maxDistance, 1 << interactableLayerIndex))
            {
            lookObject = hit.collider.gameObject;
        }
        else
        {
            lookObject = null;
        }

        //if we press the button of choice
        if (Input.GetKeyDown(KeyCode.E))
        {
            //and we're not holding anything
            if (currentlyPickedUpObject == null)
            {
                //and we are looking an interactable object
                if (lookObject != null)
                {

                    PickUpObject();
                    if (lookObject.CompareTag("OfficeKey"))
                    {
                        Debug.Log(" we got the office key!");
                        KeyScript.hasOfficeKey = true;
                        Destroy(hit.transform.gameObject, 0.35f);
                    }
                    if (lookObject.CompareTag("BackdoorKey"))
                    {
                        Debug.Log(" we got the office key!");
                        KeyScript.hasOfficeKey = true;
                        Destroy(hit.transform.gameObject, 0.35f);
                    }
                }
            }
            //if we press the pickup button and have something, we drop it
            else
            {
                BreakConnection();
            }
        }
    }
    //Velocity movement toward pickup parent and rotation
    private void FixedUpdate()
    {
        if (currentlyPickedUpObject != null)
        {
            currentDist = Vector3.Distance(pickupParent.position, pickupRB.position);
            currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
            currentSpeed *= Time.fixedDeltaTime;
            Vector3 direction = pickupParent.position - pickupRB.position;
            pickupRB.velocity = direction.normalized * currentSpeed;
            //Rotation
            lookRot = Quaternion.LookRotation(mainCamera.transform.position - pickupRB.position);
            lookRot = Quaternion.Slerp(mainCamera.transform.rotation, lookRot, rotationSpeed * Time.fixedDeltaTime);
            pickupRB.MoveRotation(lookRot);
        }

    }
    //Release the object
    public void BreakConnection()
    {
        pickupRB.constraints = RigidbodyConstraints.None;
        currentlyPickedUpObject = null;
        physicsObject.pickedUp = false;
        currentDist = 0;
    }
    public void PickUpObject()
    {
        physicsObject = lookObject.GetComponentInChildren<PhysicsObject>();
        currentlyPickedUpObject = lookObject;
        pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
        pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
        physicsObject.playerInteractions = this;
        StartCoroutine(physicsObject.PickUp());
    }
}

Well, get rid of your layermask or at least you have to include the layers that may block your pickup, like the wall. You would analyse the actual object your raycast hit to determine if it can be picked up or not.

I know very basic code, but if what @Bunny83 said doesn’t work, try making an invisible gameobject that the player collides against when they try to grab something