One direction SpotLight with PointerLockControls in Threejs

Viewed 41

I'm trying to attach a directional flashlight to the camera (controls object) so the beam is always where center of the screen is aiming at. This is my code:

controls = new PointerLockControls( camera, document.body );
var flashlight = new THREE.SpotLight(0xffffff, 7, 50, 0.8*Math.PI);
controls.getObject().add(flashlight);
controls.getObject().add(flashlight.target);

but the result is the light around camera, not a straight beam. (see pic below)

Flashlight beam

so how can I achieve a directional beam?

//edit, added Group.

controls = new PointerLockControls( camera, document.body );
var flashlight = new THREE.SpotLight(0xffffff, 7, 50, 0.8*Math.PI);
// camera.add(flashlight);
// camera.add(flashlight.target);
const group = new THREE.Group();
group.add(camera);
group.add(flashlight);  
scene.add(group);
1 Answers

You could use a THREE.Group to group objects together

const group = new THREE.Group();
group.add(camera);
group.add(flashlight);

// Finally, we add the group to our controls
controls = new PointerLockControls( group, document.body );

Then you can add the group to your scene, and control the group as a whole. The camera and flashlight will rotate and translate together.

Related