Getting Started - Chapter 8 - Follow That Character

Follow That Character

Without using parenting we can also track the characters movements with a FollowCamera.

We give a FollowCamera a start position and a target to follow and a goal position from which to view the target.

We create the FollowCamera with a name, start position and the optional scene parameter.

const camera = new BABYLON.FollowCamera("FollowCam", new BABYLON.Vector3(-6, 0, 0), scene);

We then set the goal: height above the the center of the target;

camera.heightOffset = 8;

radial distance from target plus height offset;

camera.radius = 1;

rotation, in radians, center of target in x y plane;

camera.rotationOffset = 0;

acceleration in moving from current to goal position;

camera.cameraAcceleration = 0.005

speed at which acceleration is halted

camera.maxCameraSpeed = 10

Of course we also attach the camera to the canvas

camera.attachControl(canvas, true);

Finally we set the target

camera.lockedTarget = targetMesh;
Follow The Character

Having created a 3D world, albeit just a small village, it would be good to see it in all its 3D glory. All you need is a 3D viewer whether a simple one such as the Google Carboard or a high tech one and the appropriate camera from Babylon.js.