Making the camera follow an object

When you are creating games or visualizations with many moving objects, you might want to have the camera follow an object around. Normally, when you create a camera, it points to a single position and shows you the scene that falls within its field of view. In this recipe, we'll explain how you can create a camera that can follow any of your objects around.

Getting ready

This recipe only makes use of core Three.js functions, so there isn't any need to include external JavaScript libraries in your source code. If you want to see the final result of this recipe, you can open 03.01-camera-follow-object.html in your browser, and you'll see something similar to what is shown in the following screenshot:

In this example, ...

Get Three.js Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.