FollowCamera
The FollowCamera tracks a target object while maintaining a specified offset, perfect for third-person games:
// Create target object
const player = BABYLON.MeshBuilder.CreateBox("player", {size: 1}, scene);
// Create FollowCamera
const camera = new BABYLON.FollowCamera("followCam",
new BABYLON.Vector3(0, 10, -10), // Initial position
scene);
// Configure follow behavior
camera.lockedTarget = player; // Target to follow
camera.radius = 10; // Distance from target
camera.heightOffset = 5; // Height above target
camera.rotationOffset = 180; // Rotation around target in degrees
camera.cameraAcceleration = 0.05; // Camera acceleration
camera.maxCameraSpeed = 10; // Maximum speed of camera
// Animate the player to see the camera follow
scene.onBeforeRenderObservable.add(() => {
player.position.x = Math.sin(Date.now() / 1000) * 5;
});