Book Image

Three.js Cookbook

By : Jos Dirksen
Book Image

Three.js Cookbook

By: Jos Dirksen

Overview of this book

Table of Contents (15 chapters)
Three.js Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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, you can see that the camera is focused on the sphere. As the sphere moves across the scene, the camera moves around to stay focused on the position of the sphere.

How to do it...

For this recipe, we only need to take three simple steps:

  1. The first...