Raycaster only working correctly when adding boxhelper to the scene

I’m trying to make an fbx clickable. I loaded the fbx, created a box from object so the box matches the loaded fbx. I created a boxhelper based on that box, gave it a name and added that to the scene and to a custom “meshColliders” array. When I use rayCaster.intersectObjects(meshColliders), I can succefully extract the boxHelper name I’m using to handle the click and trigger the event.

The problem is that there’s always a boxHelper visible in the scene. So I tried not adding the boxHelper to the scene so it’s only added to that meshColliders array, what I thought would happen was that it simply does not show the boxHelper but still work as planned, but the fact that I don’t add it to the scene makes the event trigger wherever I click instead of only when I actually click on the fbx.

How can this happen? And what’s a good practice for making fbx’s clickable. Adding the fbx child geometry or boundingbox to the meshColliders array does not work.

Instead of using an instance of BoxHelper, try to use Box3 instead. Create it like this:

const box = new THREE.Box3().setFromObject( fbx ); // create an AABB from your FBX model

It’s not necessary to add box to your scene. Just do this:

if ( raycaster.ray.intersectsBox( box ) === true ) {

   // your FBX was clicked

} 
2 Likes

Thanks for your reaction. Is there also an alternative to support an array of boxes or should I use a for loop?

No, you have to use a loop. :slight_smile:

3 Likes