How to make smooth mesh lookAt?

I have two objects: mesh.position, mesh.rotation and lookPoint: Vector3. I need to make slow and smooth transition between two rotations so I can’t just use mesh.lookAt( point ).
How can I find target rotation for the mesh object?

1 Like

Hello! I would get the rotation matrix of the two orientations by using the Matrix LookAt: Matrix | Babylon.js Documentation, convert to Quaternions: Quaternion | Babylon.js Documentation and interpolate by using Slerp: Quaternion | Babylon.js Documentation

2 Likes

Thanks! That’s the good idea! Ended up with the following code using your links. I’ll put my solution here in case of someone will be looking for similar question.

const lookAt = BABYLON.Matrix.LookAtLH(
	followerObj.position,
	targetObj.position,
	BABYLON.Vector3.Up()
).invert();
followerObj.rotationQuaternion = BABYLON.Quaternion.FromRotationMatrix( lookAt );

So now we can just use BABYLON.Quaternion.Slerp( rotationFrom, rotationTo, dt ).

7 Likes

Hi Sir I am having problem with mesh.lookAt… It works fine if the mesh has no parent and I do this mesh.lookAt(my character position). However, if I set the mesh.parent = skeleton[3] and do mesh.lookAt again the mesh will no longer look directly to me it will lookat somewhere else please help me :frowning:

A Playground link would be helpful here :slight_smile:

Mesh lookAt will orient a mesh independently of the hierarchy which can be translating scaling and rotating in infinite ways.

You either need to remove all parenting when doing this or account for the parent world matrix in your computation.