# pickWithRay vs intersectsMeshes, best approach?

**URL:** <https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497>\
**Category:** Questions\
**Tags:** camera, ray\
**Created:** [January 18, 2023, 3:07pm UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497 "2023-01-18T15:07:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [January 18, 2023, 3:07pm UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/1 "2023-01-18T15:07:38Z")

</div>

Dears,  
I was for the first time looking into selecting meshes from a ray casted from a first-person camera.  
I managed to achieve my goal… but then, I’m a bit confused towards what is the best approach.

In short, I cast a ray with a limited length from a caster(mesh) attached to my camera.  
When the ray intersects (or picks) with a pickable object, I want to get the name of this object.

This is where I realized that I was unable to get the name from this method (intersectsMeshes), probably because of my low coding skills? Don’t know.

I found this related to my question topic but it doesn’t bring any answer as to can you or can’t you get the name of a mesh from this method:

> [@Get the name of the mesh(es) a mesh is intersecting](https://forum.babylonjs.com/t/get-the-name-of-the-mesh-es-a-mesh-is-intersecting/6050/9):
>
> This means I’m gonna have to get the positions of all of the sub-meshes in the map mesh, aren’t I? Nice explanation, I haven’t really used arrays.

In the end, I went for two alternate solutions both using pickWithRay and pickedMesh.

There’s this one solution that uses a function and creates a new ray with each call

> **[Babylon.js Playground](https://playground.babylonjs.com/#KNE0O%231971)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

and then, there’s this other solution where I use a combination of intersects and pick and the ray is created only once.

> **[Babylon.js Playground](https://playground.babylonjs.com/#KNE0O%231970)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

**Now, the question is:**  
Is there any difference in terms of performance between these two options?… and then, is any of these 2 methods the correct approach or else, how should I do it?

Thanks in advance and meanwhile, have a great day 🕶

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 18, 2023, 5:00pm UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/2 "2023-01-18T17:00:24Z")

</div>

Let me add @Cedric who is awesome with those kind of “design” questions

---

<div class="post-metadata">

**Author:** ![Takemura](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/takemura/32/25836_2.png) [@Takemura](https://forum.babylonjs.com/u/Takemura)\
**Post date:** [January 18, 2023, 5:14pm UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/3 "2023-01-18T17:14:27Z")

</div>

I would go for the first approach, because on your second solution you check for intersection twice and it goes through all meshes instead of breaking after first intersection. Additionally it requires the RayHelper which would kill perfomance. Also I optimized your code a bit, here my PG:

> **[Babylon.js Playground](https://playground.babylonjs.com/#7GH6F8%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

If you want to get name on second method you can use:

```auto
hitInfo[0].pickedMesh.name

```

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [January 19, 2023, 8:31am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/4 "2023-01-19T08:31:27Z")

</div>

Thanks a million 🤗. I believe this is the way to go. 👍

You know, I thought that this solution (#1) I got from the featured PG in the doc was kind of funny.  
Only this morning I realized that, when using the helper, it does indeed create a new ray with each runtime and never dispose of the old one. I don’t know why this method is even featured.

As for my pseudo-solution #2, yes, you are right. It does indeed check on twice. Once with intersect and once with pick. The reason is that at first I wanted to use only intersect. Intersect sends a single callback when you start intersecting with the mesh and a single callback when the intersection ends. I found this ‘in-out’ behavior handy to use but as I said, I wasn’t able to get the name of the mesh so I added the pick method to it.

In the end, I believe I can deal with the callback from the pick method. Not a big deal. So again thanks a lot for the optimized code with included meshes array.

I think this revised version you gave me for ray picking from the camera would deserve to be featured in the doc. @Cedric @carolhmj . What do yours think? This method looks to me like something that could be [added here](https://doc.babylonjs.com/features/featuresDeepDive/mesh/interactions/picking_collisions#raycasts).

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [January 19, 2023, 10:06am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/5 "2023-01-19T10:06:30Z")

</div>

As a rule of thumb, I always think the best is to do a small benchmark. How much time and memory does it take to do 10000 raycasts? Also, stopping after the first hit has a different impact Vs shortest hit. And if your scene is a bit complex but some geometry overlap, you mays have ‘wrong’ results. That’s something to take care of as well.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [January 19, 2023, 10:36am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/6 "2023-01-19T10:36:33Z")

</div>

@Takemura @Cedric  
I actually would have one more question regarding the slick version I marked as a solution.  
**Question is:** With this method, can we use the rayHelper? I didn’t find anything to attach the helper to the camera. Does it have to be a mesh?  
Before checking out the marked as a solution above I actually opened what I believe was your first version revision @Takemura. The one keeping with the mesh casting the ray and adding an array for included meshes. There was just a little bug with the return (false) causing only the first mesh from the array to return(true). I quickly changed that and ended up with this:

> **[Babylon.js Playground](https://playground.babylonjs.com/#KNE0O%231974)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Would yours say that this version can also be ‘recommendable’ if you want to use the helper?  
Though since I don’t want to show the ray in the end, I will still go with the ‘marked as solution’ provided and thanks again for that. 😃 😃  
Meanwhile, have a great day 🕶

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [January 19, 2023, 10:41am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/7 "2023-01-19T10:41:32Z")

</div>

From a quick look, it looks like Attaching to a Node is possible. it only needs the world matrix or I missed smth.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [January 19, 2023, 10:42am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/8 "2023-01-19T10:42:44Z")

</div>

All I get from the API is that:

> **[RayHelper | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/BABYLON.RayHelper)**
>
> As raycast might be hard to debug, the RayHelper can help rendering the different raysin order to better appreciate the issue one might have. \[API\]

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [January 19, 2023, 10:44am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/9 "2023-01-19T10:44:36Z")

</div>

Sorry, I wasn’t clear 🙂  
It looks like it’s possible to add functionnality and API to attach to a Node. The code is not there, obviously but it doesn’t seem hard to add it. Unless I missed a mesh functionnality.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [January 19, 2023, 10:45am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/10 "2023-01-19T10:45:59Z")

</div>

But then if so, I guess it’s easier to parent a mesh or an abstractMesh to the camera as a ray caster, no?  
Would it change anything in terms of performance?

---

<div class="post-metadata">

**Author:** ![Takemura](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/takemura/32/25836_2.png) [@Takemura](https://forum.babylonjs.com/u/Takemura)\
**Post date:** [January 19, 2023, 10:49am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/11 "2023-01-19T10:49:00Z")

</div>

Isn’t RayHelper and its attachToMesh only for debug purposes? If you need one with my method you can do like in my current PG above.

And yes my first version was less optimized than the current, it used an additional mesh with camera parent to set ray position and direction by mesh forward, but directly using camera forwardray would be more comfortable and more optimized, I think? If you want to check again just remove #1 on my PG link to view old version.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [January 19, 2023, 10:53am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/12 "2023-01-19T10:53:01Z")

</div>

Yes, I’m just tackling it a bit. As I said, I will use your slick version that seems totally appropriate.  
But think of a title like ‘WatchDogs’ where you cast a visible ray from the character to the target.  
I know the rayHelper is mainly a debug feature but it actually uses the lineSystem to draw a line from the origin to the target and this is pretty much what you would do in case you want to show a ray, no?

**Edit:** Forget the above. There will always be other necessary tweaks and I guess it’s easier and more convenient in this case to create your own. So it makes sense that the helper remains just a debug tool.  
On the other hand, for debugging purpose, it would make sense that the helper could also be used from the camera (since the ray can be casted from the camera). Anyways, enough with that now. I got what I wanted and thanks again for your invaluable input on this.

---

<div class="post-metadata">

**Author:** ![Takemura](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/takemura/32/25836_2.png) [@Takemura](https://forum.babylonjs.com/u/Takemura)\
**Post date:** [January 19, 2023, 11:13am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/13 "2023-01-19T11:13:13Z")

</div>

I guess in this case, you can use RayHelper, because I doubt you would benefit that much more from creating your own visible ray cast by custom line mesh. Also you would have to do a bit of work to get all the information that babylon ray cast and helper provide.

> [@mawa](#):
>
> it would make sense that the helper could also be used from the camera (since the ray can be casted from the camera)

This should be the case if you set ray helper on camera.getForwardRay()?

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [January 19, 2023, 11:17am UTC](https://forum.babylonjs.com/t/pickwithray-vs-intersectsmeshes-best-approach/37497/14 "2023-01-19T11:17:55Z")

</div>

> [@Takemura](#):
>
> This should be the case if you set ray helper on camera.getForwardRay()?

I might give it a try later but lets just end this here for now. I don’t intend to use it at this moment but hopefully this discussion will make a starting point for someone facing with a similar use case.
