Move camera to object

   // get the position of the avatar
    const avatarWorldPosition = new Vector3();
    avatarPos.getWorldPosition(avatarWorldPosition);
    // get the position of the camera
    const cameraWorldPos = new Vector3();
    camera.getWorldPosition(cameraWorldPos);
    // get the direction vector from the camera to the avatar
    const pos = cameraWorldPos.clone().sub(avatarWorldPosition).normalize();
    // get the distance from the camera to the avatar
    const distance = cameraWorldPos.distanceTo(avatarWorldPosition);
    // move the camera along the direction vector with lerping
    camera.translateOnAxis(pos, lerp(0, distance, delta));

I am using this code to move camera to object. However, this causes camera to move opposite direction of the object. Can anyone explain where i am wrong ? Thank you.

NOTE: I am having trouble to cover projections matrixes etc. Is there a proper content that i can read or watch. Thanks again.

  1. To calculate vector from pointA to pointB, subtract pointB from pointA (theoretical explanation, but feel free to just memorise that it’s always end-minus-start.)

  2. You may find camera-controls quite useful.

  1. Not once in the ancient history of years of three.js usage have I had a need of doing anything with the projection or view matrices, except for calling PerspectiveCamera.updateProjectionMatrix to update it when the canvas size changes and in specific shader cases. Unless you really love maths - there isn’t really much point in memorising how and why these matrices work.
1 Like

I’d say view matrix comes up fairly often, but projection is indeed not something you need to mess with, @tolga_isik. There are situations when you would have to, but I’ve never been in those situations,

2 Likes