I'm trying to create a simple first person shooter in ThreeJS and am having some trouble with shooting a ray out of the camera. I am using the code for the PointerLock example code on Mr. Doob's github and am just trying to change the color of a box when I look at it and click the mouse but my code seems to just change boxes colors at random.
Here is the code for looking for collisions:
function onDocumentMouseDown( event ) {
event.preventDefault();
var vector = new THREE.Vector3(0,0,1);
projector.unprojectVector(vector, camera);
var rayCam = new THREE.Ray(camera.position, vector.sub(camera.position).normalize() );
var rayCaster = new THREE.Raycaster(rayCam.origin, rayCam.direction);
//rayCaster.ray = rayCam;
var intersects = rayCaster.intersectObjects(scene.children);
if (intersects.length) {
intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff );
}
}
My code works when you stand in one place but when you start moving the code acts as if the camera is still at the origin, where it started out. How can I modify this code to take into account the fact that I will be moving around?