How to make raycast ignore objects on layer based on distance to caster?

Hello!
I’m building a VR game using the Google VR SDK and designing navigation on a gaze based interaction (staring at a marker in space to trigger walk towards it).
I’ve managed to create a gaze navigation system that works fine, but I also want to introduce game objects that the player can interact with by gazing at them only when in close enough proximity.

I don’t want the raycast to hit the interactable object unless the player is close enough. I hoped this could be accomplished by putting all the interactable objects on a certain layer and somehow make the raycast ignore all objects on this layer unless the distance to the player (caster) is within a certain range.

I guess I could do this by adding a script to these objects that in the Update-method checks the distance to the player and then toggles it’s layer from “Ignore raycast” to “Default” if the distance is close enough, but I wonder that is a bad practise performance wise and maybe there are some better/more integrated way of doing this?

The way you described by adding certain objects to a layer sounds like it would work fine.

However, if im understanding your question correctly cant you just limit the distance of the raycast?
You can specify the length that the ray cast goes out as one of the parameter. By default its infintiy, but say you only wanted the raycast to interact with objects 1m in front of the player you could jus say something like

Physics.Raycast(transform.position, fwd, 1) //Where 1 is the length of the raycast

if this helped please remember to mark as answer.

Have a great day