In many interactive 3D applications, there’s a need for the camera to follow a specific point on a moving character — often the head. This technique allows for realistic first-person perspectives, dynamic over-the-shoulder shots, and immersive VR experiences without constantly recalculating the camera position in the render loop.
A reliable way to achieve this in Three.js is to attach the camera to a specific bone in the character’s skeleton, so the camera naturally follows all movements and animations.
Step 1: Creating the Camera
The first step is to create a perspective camera that defines how the scene is viewed.
Typical parameters include:
- Field of View (FOV): Narrower values reduce distortion and give a more natural look.
- Aspect Ratio: Usually the width-to-height ratio of the browser window.
- Near and Far Planes: The distance range within which objects are rendered.
const camera = new THREE.PerspectiveCamera( 32, window.innerWidth / window.innerHeight, 0.1, 1000 );
Step 2: Creating a Mount Point for the Camera
Instead of attaching the camera directly to the bone, it’s common to place it inside an empty group object (sometimes called a “mount point” or “camera rig”).
This extra layer acts as a buffer, making it easier to:
- Adjust the camera’s position relative to the bone.
- Apply rotations or small animations without affecting the bone itself.
- Swap cameras later if needed.
const cameraRig = new THREE.Group(); cameraRig.add(camera); cameraRig.position.set(0, 0.5, -0.2); cameraRig.rotation.set(0, Math.PI, 0);
Here, the position is adjusted so the camera sits slightly above and behind the bone’s pivot point, and rotated so it faces the intended direction.
Step 3: Locating and Attaching to the Bone
Every animated 3D character is made up of a hierarchy of objects, including bones. These bones are named during the rigging process (e.g., “Head”, “Neck”, etc.), and they control how the mesh deforms during animation.
To attach the camera rig:
- Traverse through all the objects in the character.
- Identify the bone that represents the head.
- Attach the camera rig to it.
character.traverse((object) => {
if (object.isBone && object.name === "Head") {
object.add(cameraRig);
}
});
Once attached, the camera will automatically inherit all position and rotation changes from the head bone during animations.
Advantages of This Method
- No Per-Frame Position Updates Needed
- The camera moves naturally with the bone as part of the model’s hierarchy.
- Perfect Animation Sync
- Any subtle movement in the head — nods, tilts, or shakes — is mirrored in the camera’s perspective.
- Flexibility for Multiple View Modes
- You can easily switch between a first-person view and other camera setups without losing the alignment.
- Ideal for VR and Cinematic Shots
- In VR, this method ensures the camera aligns perfectly with the virtual body’s head position.
Common Challenges and Solutions
- Camera Clipping Through Geometry
- If the camera clips into the head mesh, offset it further forward or hide the head mesh in first-person view.
- Overly Jittery Movement
- Use interpolation or smoothing functions to make camera transitions less abrupt.
- Bone Naming Differences
- Bone names vary between rigs. Always check the model’s hierarchy before attaching.
- Aspect Ratio Updates
- Handle window resizing so the view doesn’t distort:
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
Real-World Applications
- First-Person Gameplay — Let players look through the character’s eyes.
- Over-the-Shoulder Cinematics — Keep the camera behind and above the player’s head for a dramatic angle.
- VR Body Tracking — Match virtual camera placement to a tracked headset’s position on a 3D avatar.
- Simulation and Training — Provide a realistic view from a specific role or seat in a virtual environment.
Conclusion
Attaching a camera to a character’s head bone in Three.js is a robust way to create immersive viewpoints that automatically follow animations. By introducing an intermediate group object as a mount point, you gain fine control over camera positioning and orientation without altering the underlying skeleton. This method is highly adaptable, making it valuable for games, VR experiences, and cinematic sequences alike.