How do I make camera follow an object regardless of the x , y, and z rotation of the object?

I wanted to make the camera follow an object in x, y or z direction as the object moves in that direction.
Below is how I do it in the animation loop:

object.position.x +=0.01;
camera.poaition.x +=0.01;

This works fine. But the problem arises when object or parent of the object is rotated like

object.rotation.y = Math.PI/4;
object.rotation.x = Math.PI/3;

Any method to move the camera with respect to the object taking object.rotation.x,y, and z into consideration?

Thank you in advance.

You can move the camera independently, according to the position change of the object:

const cameraOffset = new Vector3(0.0, 5.0, -5.0); // NOTE Constant offset between the camera and the target

// NOTE Assuming the camera is direct child of the Scene
const objectPosition = new Vector3();


Even easier, you can just use position change of the object to move the camera:

const oldObjectPosition = new Vector3();

// NOTE Move object
object.position.x +=0.01;
object.rotation.y = Math.PI/4;
object.rotation.x = Math.PI/3;

const newObjectPosition = new Vector3();

const delta = newObjectPosition.clone().sub(oldObjectPosition);

1 Like

Thank you.