Getting Started - Chapter 8 - Follow That Character
Follow That Character
Without using parenting, we can also track the character's movements with a FollowCamera.
We give a FollowCamera a start position, 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.
import { FollowCamera } from "@babylonjs/core/Cameras/followCamera";import { Vector3 } from "@babylonjs/core/Maths/math.vector";const camera = new FollowCamera("FollowCam", new Vector3(-6, 0, 0), scene);We then set the goal: height above the center of the target;
camera.heightOffset = 8;radial distance from target plus height offset;
camera.radius = 1;rotation, in degrees, center of target in x y plane;
camera.rotationOffset = 0;acceleration in moving from current to goal position;
camera.cameraAcceleration = 0.005speed at which acceleration is halted
camera.maxCameraSpeed = 10Of course, we also attach the camera to the canvas.
camera.attachControl(canvas, true);Finally, we set the target.
camera.lockedTarget = targetMesh;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 Google Cardboard or a high-tech one, and the appropriate camera from Babylon.js.