将下面脚本直接挂在主相机上就可以了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//第三人称相机
public class ThirdPersonCamera : MonoBehaviour
{
public float distanceAway; // z轴上的距离
public float distanceUp; // 高度距离
public float smooth; // 移动平滑值
private GameObject hovercraft; // to store the hovercraft
private Vector3 targetPosition; // 相机目标位置
Transform follow;//跟随物体
void Start()
{
//需要跟随的物体
follow = GameObject.FindWithTag("Player").transform;
}
void LateUpdate()
{
//目标位置,由跟随物体的坐标+设置的高度+z轴的距离
//减去follow.forward * distanceAway 为距离物体后背的距离 关于follow.forward的用法可以查看Transform.forward文章
targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
//从当前位置到目标位置 平滑过渡
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
//相机朝向
transform.LookAt(follow);
}
}