# Issue with Raycasting threejs v109

**URL:** <https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171>\
**Category:** Questions\
**Tags:** raycaster\
**Created:** [December 3, 2019, 1:49am UTC](https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171 "2019-12-03T01:49:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tejasdotchavan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/tejasdotchavan/32/5328_2.png) [@tejasdotchavan](https://discourse.threejs.org/u/tejasdotchavan)\
**Post date:** [December 3, 2019, 1:49am UTC](https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171/1 "2019-12-03T01:49:42Z")

</div>

Hi guys, I am facing an issue with the raycaster:

I am trying to cast a ray from a few points to a plane, but it is unable to intersect with the plane no matter what 😕

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/b/b5dbddf3d6badc5b8339d806071a11e2e25b334e.png)

Ray origin from y:-2000, pointing up

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/b/b5edda1da31f238e3db2e41d37bd2332ef199550.png)

Its creating the ray correctly but returning no intersections

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/e/e510506f634f7d2c6e75ec70f774b26c42daa245.png)

The plane that I am trying to intersect is way above the ray origin, yet not getting any intersections. I have also tried casting the ray from top (origin.y:2000, ray direction pointing down) but no result.  
Any thoughts?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [December 3, 2019, 3:18pm UTC](https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171/2 "2019-12-03T15:18:54Z")

</div>

Can you please demonstrate the issue with [live example](https://jsfiddle.net/f2Lommf5/)?

Notice that `intersects.distance` is no valid code since `intersectObject()` always returns an array. And an array has no `distance` property. You normally code this like so:

```auto
if ( intersects.length > 0 ) {

    console.log( intersects[0].distance ); // log the distance of the first intersection

}

```

---

<div class="post-metadata">

**Author:** ![tejasdotchavan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/tejasdotchavan/32/5328_2.png) [@tejasdotchavan](https://discourse.threejs.org/u/tejasdotchavan)\
**Post date:** [December 4, 2019, 2:56am UTC](https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171/3 "2019-12-04T02:56:05Z")

</div>

Hi Mugen,

I figured out the issue. I wanted to do a calculation at initiation and in background, so I set

terrain.visible = false

I assumed the Raycaster calculation will not be dependent on mesh visibility, but guess not. Could you briefly explain how it works under the hood? Maybe it should not be dependent on mesh visibility?

---

<div class="post-metadata">

**Author:** ![DolphinIQ](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/dolphiniq/32/3510_2.png) [@DolphinIQ](https://discourse.threejs.org/u/DolphinIQ)\
**Post date:** [December 4, 2019, 6:39am UTC](https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171/4 "2019-12-04T06:39:52Z")

</div>

I believe you can turn the material’s `visible` flag to false, and the raycasting would work then.

Personally I feel like it’s logical that the ray doesn’t catch objects that were specifically set to not be visible by the programmer. Much less confusing than if someone were to make objects invisible and the ray still intersected them. But that’s only my opinion 🙂

---

<div class="post-metadata">

**Author:** ![tejasdotchavan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/tejasdotchavan/32/5328_2.png) [@tejasdotchavan](https://discourse.threejs.org/u/tejasdotchavan)\
**Post date:** [December 4, 2019, 6:45am UTC](https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171/5 "2019-12-04T06:45:30Z")

</div>

Will try the material’s visible flag.

but if we look at the **raycaster.intersectObject( object\_to\_be\_intersected )** method, I would assume it will check for intersection with the specified object regardless whether it is visible or not. After all, I am specifying the exact very object I am trying to intersect the ray with.

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [December 4, 2019, 9:59am UTC](https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171/6 "2019-12-04T09:59:18Z")

</div>

I completely agree with you. I’ve voted to change the behavior in the following issue at github and use `THREE.Layers` instead in order to control which objects are affected by raycasting.

> <https://github.com/mrdoob/three.js/issues/14700>
>
> There is one old problem for me, but still I can't find same issue in task tracker.
> Have a look at this...

The current workaround is setting the visibility of the material to `false` like mentioned by @DolphinIQ.

---

<div class="post-metadata">

**Author:** ![tejasdotchavan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/tejasdotchavan/32/5328_2.png) [@tejasdotchavan](https://discourse.threejs.org/u/tejasdotchavan)\
**Post date:** [December 5, 2019, 6:34am UTC](https://discourse.threejs.org/t/issue-with-raycasting-threejs-v109/11171/7 "2019-12-05T06:34:45Z")

</div>

Thanks Mugen! I will look into layers, seems very useful 🙂
