

![]()
using UnityEngine;
using System.Collections;
namespace CompleteProject
{
/// <summary>
/// 摄像机跟随
/// </summary>
public class CameraFollow : MonoBehaviour
{
/// <summary>
/// 摄像机跟随的目标
/// </summary>
public Transform target;
/// <summary>
/// 相机的移动速度
/// </summary>
public float smoothing = 5f;
/// <summary>
/// 摄像机相对于目标的偏移
/// </summary>
Vector3 offset;
void Start ()
{
//计算偏移
offset = transform.position - target.position;
}
void FixedUpdate ()
{
//计算相机要移动到的位置
Vector3 targetCamPos = target.position + offset;
//移动相机
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
}
CameraFollow