using UnityEngine; //摄像机控制器 public class PreviewSceneCamera { #region//属性 //摄像机实例 public Transform TransformInst { get; set; } //当前跟随目标 public Transform TargetTrans { get; set; } //当前pitch public float CurPitch = 32f; //当前yaw public float CurYaw = 135f; //当前跟随距离 public float CurDis = 25f; //当前Y轴偏移量 public float CurOffsetY = 2f; //当前X轴偏移量 public float CurOffsetX = 0f; #endregion #region//共有函数 public void Update() { if (TransformInst == null || TargetTrans == null) return; if (CurPitch < -89f) CurPitch = -89f; if(CurPitch > 89f) CurPitch = 89f; var targetPos = TargetTrans.position + Vector3.up * CurOffsetY; Quaternion rot = Quaternion.Euler(CurPitch, CurYaw, 0f); var selfPos = targetPos - rot * Vector3.forward * CurDis; if (CurOffsetX != 0) { var dir = (TargetTrans.position - selfPos); dir.y = 0; dir = Quaternion.AngleAxis(90, Vector3.up) * dir; dir = dir.normalized; targetPos = targetPos + dir * CurOffsetX; selfPos = targetPos - rot * Vector3.forward * CurDis; } TransformInst.position = selfPos; TransformInst.rotation = rot; } #endregion }