Make 3d raycast not go through walls

basicly im making a 3d fps game, and for interacitng i use a raycast to check the distance between the camera and an object etc etc

but raycasts go through walls, which i dont want, cuz that makes it so that if the player is close enough to a wall they can interact with stuff on the other side

example pic:

how can i make it Not this way

You haven’t provided code on how you are raycasting, so I’m going on the assumption that you’re raycasting to the object.

Put all the objects and walls in the same object group, and raycast to that group. The raycast will return the first object it hits that’s a member of that group.

oh mb here u go

Yep, my assumption was correct. So put all objects that are detectable by raycast, plus the walls, into an object group and raycast against that object group instead of the button object. You can then determine what object was first hit by looking at the group object name.

In this example, I’ve put the walls, buttons and switch objects into an object group named “RaycastGroup”: