Cast a ray only against "isvisible" objects

The unity documentation for raycasting says that a raycast “Casts a ray against all colliders in the scene.”

I only need to raycast against the colliders of objects which have renderer.isVisible == true. I’m thinking it would relieve a lot of strain on the CPU if you had a scene with a ton of colliders.

Does anyone with more unity know-how have any idea if this is possible?

Would I gain anything performance-wise if I had a script on each object (and say I have 1600 objects) which turned its collider on or off based on their own OnBecameVisible() or OnWillRenderObject() events?

Or would the only option be, implementing your own raycast/collision system which keeps track of isVisible objects on its own?

I’d suggest you using tags on your GameObjects, as you can create Layermasks to define which tag to ignore and which not. Then you just set the tag when making it visible/invisible.

Hope that does the trick, if not there are unfortunately no other way’s to tell a raycast to not even bother checking that GameObject.

Cheers, WRZ