Raycaster only works intermittently

Hi All, I’ve been really struggling to get the raycaster detecting meshes consistently.

I’m creating plane geometries on mouse click and drag, they should be detected so they’re not placed over each other, I add each plane geometry to the scene as well as an array and then the next time the mouse moves it checks if there’s already a mesh intersecting the mouse (the position of the detected plane is then rounded to a grid location so it’s not looking for the exact position but the grid position the mouse is within). Sometimes it detects the mesh is there and other times it doesn’t and creates another mesh over the top of the current one.
I’ve checked the scene and the array and swapped between them, and the meshes are in the scene according to the updated child length.

var canvasBounds = scene.rendererModelling.domElement.getBoundingClientRect();
input.mouse.pos.x = ( ( event.clientX - canvasBounds.left ) / ( canvasBounds.right - canvasBounds.left ) ) * 2 - 1;
input.mouse.pos.y = - ( ( event.clientY - canvasBounds.top ) / ( canvasBounds.bottom - canvasBounds.top) ) * 2 + 1;
//console.log("GetPoint MouseX: ",input.mouse.pos.x,", GetPoint MouseY: ",input.mouse.pos.x)
var groupTileIntersects = scene.raycaster.intersectObjects(scene.currentGroup.children, true)

I’ve also implemented and checked the canvas mouse position code suggested in an earlier thread to make sure it’s getting the correct position, and the new canvas position code is returning the same exact position as my previous code on each mouse move.

I’ve also inserted ‘scene.currentGroup.updateMatrixWorld()’ after the mesh is created to update the group it’s going into.

Every check I’ve made is saying that the new mesh is in the scene but the raycaster sometimes doesn’t see it, and other times it will.

…I’m stumped

:roll_eyes: appears that it was a version thing…updated from v97 to v105 mostly fixed it, but there is still the occasional point where the raycaster doesn’t detect a mesh intersection at the mouse position, saying there’s none.

It appears to be a timing thing where tiles are created too fast for the raycaster to recognise that they’re there. I inserted this straight after tile creation and it seems to have solved the issue:

tileInfo.tileCreate = false
setTimeout(function(){tileInfo.tileCreate = true},50)

I then made it so that the create code can only run once ‘tileInfo.tileCreate = true’

1 Like


Not exactly what you were looking for, but I feel like it could be useful nonetheless :v:

1 Like

cheers for that! Looks a lot simpler than my implementation :rofl: I’ve been making this project to learn Javascript so there’s a lot of extra bits I’ve coded that can probably be done a lot simpler.

1 Like