How to move a camera with an object?

Hi,

In the inital state my scene is composed by (the initial state in the figure below):

  • A cube whose position is (Xc, Yc, Zc)
  • An orthographic camera pointing at the center of the cube and whose position is (Xc + 100, Yc, Zc).
    image

Now, the user can move the cube as (s)he want, in every direction X, Y,Z and can rotate it around theses theres axis.

I’d like the camera move accordingly with the cube so that it still pointing at the center of the cube with a fixed distance between the cube and the camera. (the figure on the right)
The red face being
I can’t find a solution how to move the camera in this way. Does anyone know how to to this?
Thanks

One way is to add the camera to the object. In this way when the object moves and rotates, the camera will do the same, as if its is attached with a selfie stick.

var camera = new THREE.OrthographicCamera( ... );
    camera.position.set( 100, 0, 0 ); 
    camera.lookAt( 0, 0, 0 );
:
var object = new THREE.Mesh( ... );
    object.add( camera );

Demo – the central object moves and rotates and the camera always looks at its right side, all other objects are static:

https://codepen.io/boytchev/full/WNYqYLj

image

1 Like

I’m happy with the solution given above, but my user wants now to add the possibility to zoom in and out with the mouse wheel. When I try to add a OrbitControl, the camera won’t follow corecty the object… What control to be use to archive their request? thanks