Seems like I might just be overlooking a simple solution here. I have a setup with a spotlight pointing backward from the camera towards a plane geometry, as well as some randomly scattered boxes. My goal is to create a basic top-down demo. I move the camera using the WASD keys, capturing the position, resetting the Y to zero, and then making the camera focus on that position. I also adjust the light's target to match the camera's new position. The camera moves smoothly, but it seems like the light isn't changing its focus, at least as far as I can tell.
Here's how I create the light and its target:
this.playerLight = new THREE.SpotLight(0xffffff);
this.playerLight.castShadow = true;
this.playerLight.position.set(0, 40, 0);
this.spotlightTarget = new THREE.Object3D();
this.spotlightTarget.position.set(0, 0, 0);
this.playerLight.target = this.spotlightTarget;
this.playerLight.shadowCameraVisible = true;
this.scene.add(this.playerLight);
Next, here's my keyboard event handling code:
window.addEventListener("keydown", function (e) {
var moved = false;
switch ( event.keyCode ) {
case 87: // W
e.preventDefault();
_this.camera.position.x -= 0.2;
moved = true;
break;
case 65: // A
e.preventDefault();
_this.camera.position.z += 0.2;
moved = true;
break;
case 83: // S
e.preventDefault();
_this.camera.position.x += 0.2;
moved = true;
break;
case 68: // D
e.preventDefault();
_this.camera.position.z -= 0.2;
moved = true;
break;
default:
return true;
}
if (moved) {
var lookAtPos = _this.camera.position.clone();
lookAtPos.y = 0;
_this.camera.lookAt(lookAtPos);
_this.playerLight.position.x = lookAtPos.x;
_this.playerLight.position.z = lookAtPos.z;
_this.spotlightTarget.position.set(lookAtPos.x, lookAtPos.y, lookAtPos.z);
}
}, false);
Any thoughts on what might be causing this issue?