Prevent raycaster from casting through other objects

Hi there!

How can I prevent the raycaster from casting (on sphere) through other objects (cube) in the scene as you can see here.

I set up a second ‘intersectObjects’ for everything that the raycaster should not cast through. If the raycaster casts through one of these blocked objects and the ray hits an interactive object, the interaction is disabled as you can see here.

Since this is not elegant at all, can somebody help me with a better solution? :slight_smile:

If you dont have a ton of objs, put all the objects in the same array for the let intersections = raycaster.intersectObjects(array) function and when an blocking obj is hit, do nothing.
intersections[0].object is always the nearest obj hit. You can give them proper names like = 'blocking' or you can give them userData: obj.userData.blocking = true or a layer obj.userData.layer = 1 for differentiating.

1 Like

Thank you! :slight_smile: I did it with checking for the name of the blocked object.