2
votes

I'm using PointerLockControls in Three.js.

On mouse click, I want to update a sphere position to the position the camera is facing, at the same z-position of a certain object. I've read about getDirection(), but can't seem to implement it the right way. Here's what I tried:

var mouse3D = new THREE.Vector3();
mouse3D.normalize();
controls.getDirection( mouse3D );
sphere.position.x = mouse3D.x;
sphere.position.y = mouse3D.y;
sphere.position.z = object.position.z;

The z-position is fine, but x and y are so close to 0 that the sphere stays "on the ground" and doesn't go "left or right".

Any help is much appreciated!

1

1 Answers

0
votes

direction is a normalized vector (length is 1)

you have to find factor so that camera.position + direction * factor gives vector with z = object.position.z

factor = (object.position.z - camera.position.z)/direction.z

and just multiply your mouse3D vector mouse3D.multiplyScalar(factor)