实现相机跟随有许多方法,例如可以通过组件实现,也可以通过代码实现,一般通过代码实现比较灵活,所以我们通常一般都是使用代码进行实现。
首先调整摄像机的位置并创建脚本挂载在摄像机组件上,打开脚本编辑实现效果。
我们必须找到角色的Transform组件,并且需要定义相机和角色之间的偏移量。
代码实现:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollow : MonoBehaviour { // Start is called before the first frame update // 角色的 Transform 组件 public Transform ta