How to stop a raycast going through a wall (3D)

I am working on a 3d game, and I need a way to interact with objects. I’ve tried doing it by raycasts, or checking how far the player is from an object and if they’re looking at it, but the same problem occured each time: the walls, how do I stop raycasts going through them?

Have you tried to check raycast on them too and compare the distance?

Create seaparated object groups in the editor , One group with object to collide with, and the other not, assign the group object in the raycast action