I wrote my FPS controller and now I try to make a simple collision detect.

var b = new THREE.Vector3(0, 1, 0); function detectCollision() { scene.updateMatrixWorld(); raycaster.set(playerBody.position, b); var intersects = raycaster.intersectObjects(objects, true); if(intersects.length > 0) { intersects[0].object.material.color.set(0x000000); console.log('Yay!'); } } 

Reacts only to the line that I draw to check the correctness of the emitted beam. The reaction to the other objects was, but in a completely random place. I rummaged through the entire Internet yesterday, but did not understand what the problem was, please tell me what could be the matter.

    1 answer 1

    1. Add more vectors coming from the playerBody.position position. Now you have only one var b = new THREE.Vector3 (0, 1, 0), and you need an array of vectors directed to the remaining sides. Next, loop them over and substitute raycaster.set (playerBody.position, b);

    2. Make sure that the "objects" array contains objects that should participate in the collision.