I am trying to use raycasting to detect whether my mouse pointed at the object, when it didnt touch the function is working fine, but when it touch, it did stop printing out "didnt touch", while it didnt print"touch" at the same time. For the if statement i include here, there is only 2 case either === or not right? so why my code isnt working? can anyone give me some help
function animate(){
renderer.render(scene,camera);
sLightHelper.update();
rayCaster.setFromCamera(mousePosition,camera);
var intersects = rayCaster.intersectObjects(scene.children);
if ( house != undefined ){
for(let i = 0; i < intersects.length; i++){
if(intersects[0].object.id === house.ID){
console.log("touch");
}
else{
console.log("didnt touch");
}
}
console.log("finished");
}
else{
console.log('not ready')
}
}
Try to write your code like so:
if ( intersects.length > 0 ) {
for(let i = 0; i < intersects.length; i++){
if(intersects[0].object.id === house.ID){
console.log("touch");
} else{
console.log("didnt touch");
}
}
} else {
console.log("didnt touch");
}
console.log("finished");