I am attempting to keep a sphere object centered in front of a moving camera. The camera's position and rotation are being animated using lookAt()
, which is functioning correctly. However, the sphere object is not staying within the frame as desired. I have tried different approaches:
const centerSphereInCameraWithQuaternion = (sphere: THREE.Mesh, camera: THREE.PerspectiveCamera) => {
const vec = new THREE.Vector3( 0, 0, - 4 );
vec.applyQuaternion( camera.quaternion );
sphere.position.copy( vec );
}
This method causes the sphere to disappear completely. Another attempt was made with:
const centerSphereInCamera = (sphere: THREE.Mesh, camera: THREE.PerspectiveCamera) => {
sphere.position.copy( camera.position );
sphere.rotation.copy( camera.rotation );
sphere.updateMatrix();
sphere.translateZ( - 4 );
}
While this nearly works, the sphere rotates opposite to the direction of the camera's facing. Additionally, I experimented with:
sphere.position.sub(camera.position);
However, this did not yield any visible results.
I even used a camera helper to verify that the camera's world matrix is updating correctly, which it appears to be.
What mistake am I making in my approach? Is there a way to achieve this without making the mesh a child of the camera?