lookAt for child object

Viewed 42

Trying to make a CHILD of an object look at a position (which is in global/world space)

Here's what I've got, but it doesn't work. Direction of arrow seems to roam around in unpredictable directions.

Update (added example): https://jsfiddle.net/um8cxp3b/27/

makeArrowLookAt(obj, position) {
 
  // obj.lookAt(position); // works PERFECTLY, but I want a child object to lookAt...
      
  const posVector = new THREE.Vector3().copy(position); // copy
  const localVector = obj.worldToLocal(posVector); // get local position offset
  localVector.normalize(); // converts local offset to rotation... right?
      
  // Apply the localised rotation to the object
  obj.children[0].rotation.x = localVector.x;
  obj.children[0].rotation.y = localVector.y;
  obj.children[0].rotation.z = localVector.z;
}

(Note the child in this case is an arrowHelper to illustrate direction)

1 Answers

lookAt() does support transformed ancestors (assuming they are not non-uniformly-scaled). So it should be just:

obj.children[0].lookAt(position);

Assuming position is the target position in world space.

Related