I'm fairly new to three.js and trying to get a better understanding of ray casting. I have used it so far in a game to show when an object collides with another on the page which works perfectly. In the game I'm building this is to take health from the hero as it crashes into walls.
I am now trying to implement a target which when hovered over some objects it will auto shoot. However the target only registers a target hit once the object passes through the target mesh rather than when its ray is cast through it.
to further detail this, the ray is cast from the camera through the target, if the target is on a mesh (stored as an object array) then I want it to trigger a function.
within my update function I have this:
var ray = new THREE.Raycaster();
var crossHairClone = crossHair.position.clone();
var coards = {};
coards.x = crossHairClone.x
coards.y = crossHairClone.y
ray.setFromCamera(coards, camera);
var collisionResults = ray.intersectObjects( collidableMeshList );
if ( collisionResults.length > 0 ) {
console.log('Target Hit!', collisionResults)
}
The console log is only triggered when the collidableMeshList actually touches the target mesh rather than when it is aiming at it.
How do I extend the ray to pass through the target (which I thought it was already doing) so that if anything hits the ray then it triggers my console log.
Edit
I've added a URL to the game in progress. There are other wider issues with the game, my current focus is just the target ray casting.
Raycasterdoes pass through all of the meshes at which it is aiming. YourcollisionResultsshould be an array of all intersection points, with each point containing information about the intersection. - TheJim01collisionResultsis just an array of the meshes created. So as each wall is added into the scene it is added to the array and then created using thisvar collisionResults = ray.intersectObjects( collidableMeshList );the MeshList being an array of objects. For some reason the console log only happens once target makes contact with the Object, not when its in its sights - SamesJeabrookonMosueClickwhen you should be doing it every frame in your animation loop. - Xander Luciano