Make Object A always look at Object B

Just like how spot/direction light looks at it’s target and sprites look at camera , how can i make Mesh/Object3d/Group A look at Mesh/Object3d/Group B without adding any extra code in the animation loop as this object’s existence in the scene is optional and will controlled by the user

one method i found which works is to override the eye object’s updateMatrixWorld and call the eyeObject.lookAt(targetObject.position) there .

eg:

class EyeObject extends Object3d{
	constructor() {
		super()
		this.target = new Object3d()
	}

	updateMatrixWorld(force) {
		this.lookAt(this.target.position)
		super.updateWorldMatrix(force)
	}
}

is there a better way?

Function lookAt resets rotation around local z axis, so it will only work if your eye is vertical, as in if it’s a snake eye, the pupil is vertical in the world space.

You can use Object.defineProperty to overwrite quaternion, I wouldn’t call it a better way, a better way is to use the animation loop :slightly_smiling_face:

1 Like

the top side of object always pointing up works in my favor

eg: a floating head which is always upright looking at a ball moving around the floor

so lookAt is actually what i need

the “defineProperty” method of writing is something new , that’s something i’ll check out , thank you !

1 Like

Name your function onBeforeRender instead, rather than overwriting methods.

https://threejs.org/docs/#api/en/core/Object3D.onBeforeRender

1 Like

yesss , that seems easy and efficient to setup !
the doc says it only works for render-able mesh , need to confirm if it’ll work with Groups also
thanks!