# Prevent raycaster from casting through other objects

**URL:** <https://discourse.threejs.org/t/prevent-raycaster-from-casting-through-other-objects/15210>\
**Category:** Questions\
**Tags:** line-segments-and-raycaster\
**Created:** [May 14, 2020, 12:09pm UTC](https://discourse.threejs.org/t/prevent-raycaster-from-casting-through-other-objects/15210 "2020-05-14T12:09:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jedinger](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/jedinger/32/11158_2.png) [@jedinger](https://discourse.threejs.org/u/jedinger)\
**Post date:** [May 14, 2020, 12:09pm UTC](https://discourse.threejs.org/t/prevent-raycaster-from-casting-through-other-objects/15210/1 "2020-05-14T12:09:40Z")

</div>

Hi there!

How can I prevent the raycaster from casting (on sphere) through other objects (cube) in the scene as you can see [here](https://jsfiddle.net/v6puLnqg/1/).

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](https://jsfiddle.net/jqvths0b/2/).

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

---

<div class="post-metadata">

**Author:** ![Fluqz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/fluqz/32/6279_2.png) [@Fluqz](https://discourse.threejs.org/u/Fluqz)\
**Post date:** [May 14, 2020, 12:21pm UTC](https://discourse.threejs.org/t/prevent-raycaster-from-casting-through-other-objects/15210/2 "2020-05-14T12:21:58Z")

</div>

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 `obj.name = 'blocking'` or you can give them userData: `obj.userData.blocking = true` or a layer `obj.userData.layer = 1` for differentiating.

---

<div class="post-metadata">

**Author:** ![jedinger](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/jedinger/32/11158_2.png) [@jedinger](https://discourse.threejs.org/u/jedinger)\
**Post date:** [May 14, 2020, 2:18pm UTC](https://discourse.threejs.org/t/prevent-raycaster-from-casting-through-other-objects/15210/3 "2020-05-14T14:18:59Z")

</div>

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