Raycaster finds no collisions with BoxGeometry

Hello Threejs community,

I would like to detect collisions with a Raycaster.
However, no intersections are found.
Perhaps only a small mistake from my side.
I am new to the topic and already spent so much time searching for a fix.

Here the sandbox showing the problem.

Thank you for your help!

You’re casting the ray on a non-existent cube. Moving renderer.render(scene, camera); above the ray-casting code should help in this case (above raydirection declaration.)

Usually you’d do the raycasting in an event - ex. mousemove or click - after the scene is rendered and app is running and already rendering frames, so this kind of issue wouldn’t happen.

Thank you so much!
That’s the trick.

The cube is existing, but its matrix is not updated (that’s what renderer.render() does internally for all the objects in the scene).
So, it’s also an option to call cube.updateMatrixWorld() right after cube.position.y = 1.5;.

1 Like