TRACKING ANIMATED
3D OBJECTS
By Shane Brumback · · Updated · Free Three.js tutorial · Level 1
To make a Three.js camera track an animated object, move the object first, then each frame lerp the camera toward a spot relative to the object and call camera.lookAt on a smoothed copy of its position. This glossy red ball bounces around a looping path while the camera follows in chase, tower or orbit mode.
A beginner walkthrough of CatmullRomCurve3 paths, lerp smoothing, lookAt and an OrbitControls target that follows a moving object in Three.js. The chase camera behind this page is the live demo.
LIVE DEMO
The chase camera behind this page is a live Three.js scene. Launch the full-screen demo and press C or tap the camera button to switch between chase, tower and orbit modes. In orbit mode, drag to circle the ball while the camera keeps tracking it. Launch asks for a free Google sign-in first. Press Esc or Exit to return.
Free Google sign-in · C or tap to switch camera · Orbit: drag · Esc to exit
Two Jobs: Where the Camera Is and Where It Looks
Following an object is really two small jobs. Position: where should the camera stand? Behind the ball, high on a tower, or wherever you dragged it. Aim: what should it point at? The ball. Three.js gives you camera.lookAt(point) for the aim, and you move the camera position yourself.
If you snap the camera straight to the target every frame, motion looks robotic and jittery. Instead we lerp, short for linear interpolation: each frame the camera moves a fraction of the way to where it wants to be. Using 1 - Math.exp(-speed * dt) as that fraction keeps the smoothing identical on slow and fast screens.
Three Camera Modes in One Scene
Chase stands seven units behind the ball along its direction of travel, like a racing game. Tower stays on a fixed high spot and only turns its head, like a security camera or a sports broadcast. Orbit hands the camera to OrbitControls so you can drag around, but slides the controls target and the camera along with the ball so it never leaves the frame.
Want the camera to circle something that stays still instead? See camera tracking a stationary object. For one object chasing another, read how to make one object follow another.
Step-by-Step Code Breakdown
Every excerpt below is copied exactly from the full code further down.
1. Set up the scene, camera, renderer and controls
In plain words: we make a black world with fog and a red neon grid, a camera and renderer, and orbit controls that stay off until orbit mode. Create a Scene with fog, a PerspectiveCamera, a WebGLRenderer with ACES tone mapping, OrbitControls attached to renderer.domElement and disabled, a HemisphereLight and a GridHelper.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x020205);
scene.fog = new THREE.Fog(0x020205, 25, 70);
const camera = new THREE.PerspectiveCamera(55, innerWidth / innerHeight, 0.1, 300);
camera.position.set(0, 6, 14);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.enabled = false;
scene.add(new THREE.HemisphereLight(0x8899ff, 0x110000, 0.6));
const grid = new THREE.GridHelper(120, 60, 0xff2244, 0x3a3a55);
scene.add(grid);
2. Make the glossy red ball, its glow and shadow
In plain words: the star of the show is a shiny red ball with a red light that travels with it and a dark circle that fakes its shadow. Build a SphereGeometry with a clearcoat MeshPhysicalMaterial, a red PointLight, a DirectionalLight, and a transparent black CircleGeometry for a blob shadow.
const ball = new THREE.Mesh(
new THREE.SphereGeometry(0.8, 48, 32),
new THREE.MeshPhysicalMaterial({ color: 0xff1a1a, emissive: 0x550000, roughness: 0.15, metalness: 0.2, clearcoat: 1 })
);
scene.add(ball);
const glow = new THREE.PointLight(0xff2a2a, 40, 14, 2);
scene.add(glow);
const keyLight = new THREE.DirectionalLight(0xffffff, 1.5);
keyLight.position.set(-5, 10, 5);
scene.add(keyLight);
const shadow = new THREE.Mesh(
new THREE.CircleGeometry(0.9, 32),
new THREE.MeshBasicMaterial({ color: 0x000000, transparent: true, opacity: 0.6 })
);
shadow.rotation.x = -Math.PI / 2;
shadow.position.y = 0.02;
scene.add(shadow);
3. Draw a looping path
In plain words: we place eight points around the floor and let Three.js join them into a smooth closed loop, then draw it as a faint line. Create a closed CatmullRomCurve3 from eight Vector3 points and draw it with a Line built from getPoints.
const path = new THREE.CatmullRomCurve3([
new THREE.Vector3(0, 0, 0), new THREE.Vector3(12, 0, -8), new THREE.Vector3(22, 0, 4),
new THREE.Vector3(10, 0, 16), new THREE.Vector3(-4, 0, 10), new THREE.Vector3(-18, 0, 18),
new THREE.Vector3(-24, 0, 0), new THREE.Vector3(-12, 0, -14)
], true);
const trail = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(path.getPoints(300)),
new THREE.LineBasicMaterial({ color: 0x661018 })
);
trail.position.y = 0.03;
scene.add(trail);
4. Animate the ball along the path
In plain words: time decides how far around the loop the ball is; we also bounce it up and down, spin it and squeeze the shadow when it is high. Use getPointAt and getTangentAt with a 0 to 1 lap value, add an absolute sine bounce, roll the ball, and move the shadow and light with it.
const LAP_SECONDS = 30;
const tangent = new THREE.Vector3();
function moveBall(t) {
const u = (t / LAP_SECONDS) % 1;
path.getPointAt(u, ball.position);
path.getTangentAt(u, tangent);
const bounce = Math.abs(Math.sin(t * 3));
ball.position.y = 0.8 + bounce * 1.6;
ball.rotation.x -= 0.06;
shadow.position.set(ball.position.x, 0.02, ball.position.z);
shadow.scale.setScalar(1.1 - bounce * 0.45);
glow.position.copy(ball.position);
}
5. Define the camera modes
In plain words: we list the three modes and write a switch that turns orbit controls on only in orbit mode and updates the button text. Keep a MODES array, a desired position vector, a smoothed look target and a fixed tower position, and a setMode function that toggles controls.enabled.
const MODES = ['chase', 'tower', 'orbit'];
let mode = 'chase';
const desired = new THREE.Vector3();
const lookTarget = new THREE.Vector3();
const TOWER = new THREE.Vector3(0, 14, 24);
function setMode(next) {
mode = next;
controls.enabled = mode === 'orbit';
if (mode === 'orbit') controls.target.copy(ball.position);
modeLabel.textContent = 'Camera: ' + mode + ' (press C or tap to switch)';
}
6. Follow the ball every frame
In plain words: chase mode slides the camera behind the ball, tower mode stays put, orbit mode moves the camera and its target along with the ball, and every mode keeps aiming at it smoothly. Compute a frame-rate independent smoothing factor, lerp toward a point behind the tangent in chase, toward the tower in tower mode, shift camera and controls.target by the same delta in orbit, otherwise lerp a look target and call camera.lookAt.
function updateCamera(dt) {
const smooth = 1 - Math.exp(-3 * dt);
if (mode === 'chase') {
desired.copy(tangent).multiplyScalar(-7).add(ball.position);
desired.y = 4;
camera.position.lerp(desired, smooth);
} else if (mode === 'tower') {
camera.position.lerp(TOWER, smooth);
}
if (mode === 'orbit') {
lookTarget.copy(controls.target);
controls.target.lerp(ball.position, smooth * 2);
camera.position.add(lookTarget.sub(controls.target).negate());
lookTarget.copy(controls.target);
controls.update();
} else {
lookTarget.lerp(ball.position, 1 - Math.exp(-8 * dt));
camera.lookAt(lookTarget);
}
}
7. Add the mode button and run the loop
In plain words: a button and the C key cycle the modes, and every frame we move the ball, update the camera and draw. Create a fixed-position button, cycle modes on click or the C key, then run a Clock-driven render loop and handle resize.
const modeLabel = document.createElement('button');
modeLabel.style.cssText = 'position:fixed;left:16px;bottom:16px;padding:10px 16px;border-radius:10px;background:rgba(0,0,0,0.65);color:#ff5a6a;font:600 15px system-ui,sans-serif;border:1px solid rgba(255,60,80,0.5);cursor:pointer;z-index:5;';
document.body.appendChild(modeLabel);
const nextMode = () => setMode(MODES[(MODES.indexOf(mode) + 1) % MODES.length]);
modeLabel.addEventListener('click', nextMode);
addEventListener('keydown', (e) => { if (e.code === 'KeyC') nextMode(); });
setMode('chase');
const clock = new THREE.Clock();
function render() {
requestAnimationFrame(render);
const dt = Math.min(clock.getDelta(), 0.05);
moveBall(clock.elapsedTime);
updateCamera(dt);
renderer.render(scene, camera);
}
render();
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
Order matters: move the object first, then the camera, then render. If you move the camera before the object, it is always one frame behind and the ball seems to shake.
The Full Code
Paste this module into a page with the same import map as this one (Three.js 0.160 from unpkg). No models or images to download.
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x020205);
scene.fog = new THREE.Fog(0x020205, 25, 70);
const camera = new THREE.PerspectiveCamera(55, innerWidth / innerHeight, 0.1, 300);
camera.position.set(0, 6, 14);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.enabled = false;
scene.add(new THREE.HemisphereLight(0x8899ff, 0x110000, 0.6));
const grid = new THREE.GridHelper(120, 60, 0xff2244, 0x3a3a55);
scene.add(grid);
const ball = new THREE.Mesh(
new THREE.SphereGeometry(0.8, 48, 32),
new THREE.MeshPhysicalMaterial({ color: 0xff1a1a, emissive: 0x550000, roughness: 0.15, metalness: 0.2, clearcoat: 1 })
);
scene.add(ball);
const glow = new THREE.PointLight(0xff2a2a, 40, 14, 2);
scene.add(glow);
const keyLight = new THREE.DirectionalLight(0xffffff, 1.5);
keyLight.position.set(-5, 10, 5);
scene.add(keyLight);
const shadow = new THREE.Mesh(
new THREE.CircleGeometry(0.9, 32),
new THREE.MeshBasicMaterial({ color: 0x000000, transparent: true, opacity: 0.6 })
);
shadow.rotation.x = -Math.PI / 2;
shadow.position.y = 0.02;
scene.add(shadow);
const path = new THREE.CatmullRomCurve3([
new THREE.Vector3(0, 0, 0), new THREE.Vector3(12, 0, -8), new THREE.Vector3(22, 0, 4),
new THREE.Vector3(10, 0, 16), new THREE.Vector3(-4, 0, 10), new THREE.Vector3(-18, 0, 18),
new THREE.Vector3(-24, 0, 0), new THREE.Vector3(-12, 0, -14)
], true);
const trail = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(path.getPoints(300)),
new THREE.LineBasicMaterial({ color: 0x661018 })
);
trail.position.y = 0.03;
scene.add(trail);
const LAP_SECONDS = 30;
const tangent = new THREE.Vector3();
function moveBall(t) {
const u = (t / LAP_SECONDS) % 1;
path.getPointAt(u, ball.position);
path.getTangentAt(u, tangent);
const bounce = Math.abs(Math.sin(t * 3));
ball.position.y = 0.8 + bounce * 1.6;
ball.rotation.x -= 0.06;
shadow.position.set(ball.position.x, 0.02, ball.position.z);
shadow.scale.setScalar(1.1 - bounce * 0.45);
glow.position.copy(ball.position);
}
const MODES = ['chase', 'tower', 'orbit'];
let mode = 'chase';
const desired = new THREE.Vector3();
const lookTarget = new THREE.Vector3();
const TOWER = new THREE.Vector3(0, 14, 24);
function setMode(next) {
mode = next;
controls.enabled = mode === 'orbit';
if (mode === 'orbit') controls.target.copy(ball.position);
modeLabel.textContent = 'Camera: ' + mode + ' (press C or tap to switch)';
}
function updateCamera(dt) {
const smooth = 1 - Math.exp(-3 * dt);
if (mode === 'chase') {
desired.copy(tangent).multiplyScalar(-7).add(ball.position);
desired.y = 4;
camera.position.lerp(desired, smooth);
} else if (mode === 'tower') {
camera.position.lerp(TOWER, smooth);
}
if (mode === 'orbit') {
lookTarget.copy(controls.target);
controls.target.lerp(ball.position, smooth * 2);
camera.position.add(lookTarget.sub(controls.target).negate());
lookTarget.copy(controls.target);
controls.update();
} else {
lookTarget.lerp(ball.position, 1 - Math.exp(-8 * dt));
camera.lookAt(lookTarget);
}
}
const modeLabel = document.createElement('button');
modeLabel.style.cssText = 'position:fixed;left:16px;bottom:16px;padding:10px 16px;border-radius:10px;background:rgba(0,0,0,0.65);color:#ff5a6a;font:600 15px system-ui,sans-serif;border:1px solid rgba(255,60,80,0.5);cursor:pointer;z-index:5;';
document.body.appendChild(modeLabel);
const nextMode = () => setMode(MODES[(MODES.indexOf(mode) + 1) % MODES.length]);
modeLabel.addEventListener('click', nextMode);
addEventListener('keydown', (e) => { if (e.code === 'KeyC') nextMode(); });
setMode('chase');
const clock = new THREE.Clock();
function render() {
requestAnimationFrame(render);
const dt = Math.min(clock.getDelta(), 0.05);
moveBall(clock.elapsedTime);
updateCamera(dt);
renderer.render(scene, camera);
}
render();
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
Frequently Asked Questions
How do you make the camera follow an object in Three.js?
Each frame, after moving the object, compute where the camera should be relative to it, move the camera part of the way there with position.lerp, and call camera.lookAt on the object position. Lerping makes the follow smooth instead of rigid.
What does lerp do in Three.js?
Vector3.lerp(target, alpha) moves a vector a fraction alpha of the way toward target. With alpha = 1 - Math.exp(-speed * dt) the smoothing is independent of frame rate, so the camera feels the same on every screen.
How do I keep OrbitControls focused on a moving object?
Set controls.target to the object position every frame and move the camera by the same amount the target moved, then call controls.update(). The user can still drag to orbit while the view follows the object.
How do I make a chase camera?
Get the direction the object is moving, for example from getTangentAt on a curve, place the camera a few units behind along the opposite direction and a little above, lerp toward that point and look at the object.
Why does my camera jitter when it follows an object?
Usually the camera updates before the object moves, or it snaps directly to the target. Update the object first, then lerp the camera, and smooth the lookAt target too.
Why do I need to sign in to launch the live demo?
The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site.
Play It and Keep Learning
Smooth follow cameras like this frame the action in Super Soldier Battle, a free Three.js browser game. Browse every Three.js tutorial, read the articles, or search the site.
Need Three.js or WebGL work for your site or game? I take freelance and contract work: contact me.
Play Game