Allows the camera to smoothly follow the object and the camera to follow the object
Sometimes when we are playing a third-person game and want the camera to smoothly follow the object, the following code can quickly achieve this effect.
Using UnityEngine; using System. collections; public class CameraMover: MonoBehaviour {public Transform follow; // tansform public float distanceAway = 5.0f; // indicates the distance from public float distanceUp = 2.0f to the rear of the target; // The height of public float smooth = 1.0f above the target; // The interpolation coefficient private Vector3 camPosition; // the position of the target void LateUpdate () {// calculate the position where the camera should be located, camPosition = follow. position + Vector3.up * distanceUp-follow. forward * distanceAway; // interpolation to the camera's expected position transform. position = Vector3.Lerp (transform. position, camPosition, smooth * Time. deltaTime); // a function in tranform, used to make an object forward to the target transform. lookAt (follow); // understood as looking at the target }}