| 1.首先给摄像机添加一个脚本 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
    private Transform targer;//目标
    private Vector3 offset;//偏移量
    private Vector2 velocity;//当前速度,此值由函数在每次调用时进行修改。
    private void Update()
    {
        if (targer == null && GameObject.FindGameObjectWithTag("Player"))//查找目标,该判断可避免重复赋值
        {
            targer = GameObject.FindGameObjectWithTag("Player").transform;
            offset = transform.position - targer.position;
        }
    }
    private void FixedUpdate()//1
    {
        if (targer != null)
        {
            float posX = Mathf.SmoothDamp(transform.position.x, targer.position.x - offset.x, ref velocity.x, 0.05f);//2
            float posY = Mathf.SmoothDamp(transform.position.y, targer.position.y - offset.y, ref velocity.y, 0.05f);
            transform.position = new Vector3(posX, posY, transform.position.z);
        }
    }
}
 //1.FixedUpdate()固定更新事件,执行N次,0.02秒执行一次。所有物理组件相关的更新都在这个事件中处理。 //2.Mathf.SmoothDamp 随时间推移将一个值逐渐改变为所需目标。 值通过某个类似于弹簧-阻尼的函数(它从不超过目标)进行平滑。? 属性: current-当前位置。 target-尝试达到的目标。 currentVelocity-当前速度,此值由函数在每次调用时进行修改。 smoothTime-达到目标所需的近似时间。值越小,达到目标的速度越快。 2.给要跟随的玩家添加一个tag 
 摄像机的跟随就完成啦 |