Spotlight can't set target correctly

Hi, I’m trying to set up the spotlights, but target always directs to (0,0,0).
I’ve tried

  • spotlight.target.position.set(x,y,z);
  • spotLight.target = object;
  • spotLight.lookAt( object.position ); ← this direct to weired point(not expected direction)

and also I’ve tried

scene.add( spotLight );
scene.add( spotLight.target );

but it didn’t work…
I would appreciate if anyone had any solution.
Thanks!

1 Like

Try it with this basic template:

https://jsfiddle.net/f2Lommf5/10847/

Also see:

4 Likes

It works!!! Thank you sooooo much Mugen87 !!!

If anyone wants to see the spotlight animate with the spotlight helper, you can make the following changes to @Mugen87 's jsfiddle.

// Add this at the end to declare `spotLightHelper`
let camera, scene, renderer, clock, spotLight, spotLightHelper;

init();
animate();

...

	scene.add( spotLight.target ); // add target to the scene

// Add this to add a SpotLightHelper
	spotLightHelper = new THREE.SpotLightHelper(spotLight, 0XFF00FF)

	scene.add(spotLightHelper);
...
	spotLight.target.position.z = Math.cos( time );
// Add this in animate() to make sure it animates.
    if (spotLightHelper) spotLightHelper.update();
...