一个游戏中可能会有各种类型的镜头,例如有时候是第一人称,有时是第三人称,有时又会给个特写等等,因此可以定义一个镜头类型枚举,在不同的场合进行切换,管理起来很方便。
CameraManager.cs
1 using UnityEngine; 2 using System.Collections; 3 4 //镜头类型 5 public enum CameraType 6 { 7 NotFollow, //不跟随(可以自由设置镜头) 8 FixedFollow, //固定跟随 9 SmoothFollow, //平滑跟随 10 } 11 12 public class CameraManager : MonoSingletion<CameraManager> { 13 14 private Camera camera; 15 private Transform cameraTra; 16 private Transform targetTra; 17 private CameraType cameraType; 18 19 //固定跟随参数 20 private Vector3 fixedPosOffset;//位置偏移 21 22 //设置镜头 23 public void SetCamera(Camera camera) 24 { 25 this.camera = camera; 26 cameraTra = camera.gameObject.transform; 27 } 28 29 //设置目标 30 public void SetTarget(GameObject go) 31 { 32 targetTra = go.transform; 33 } 34 35 private void LateUpdate() 36 { 37 if ((cameraTra == null) || (targetTra == null)) 38 { 39 return; 40 } 41 42 switch (cameraType) 43 { 44 case CameraType.NotFollow: 45 return; 46 case CameraType.FixedFollow: 47 FixedFollow(); 48 break; 49 case CameraType.SmoothFollow: 50 break; 51 default: 52 break; 53 } 54 } 55 56 //-----------------------------------------------不跟随 start 57 public void SetNotFollow() 58 { 59 cameraType = CameraType.NotFollow; 60 } 61 //-----------------------------------------------不跟随 end 62 63 //-----------------------------------------------固定跟随 start 64 public void SetFixedFollow(Vector3 fixedPosOffset, Vector3 rot) 65 { 66 cameraType = CameraType.FixedFollow; 67 this.fixedPosOffset = fixedPosOffset; 68 cameraTra.localEulerAngles = rot; 69 } 70 71 private void FixedFollow() 72 { 73 cameraTra.position = targetTra.position + fixedPosOffset; 74 } 75 //-----------------------------------------------固定跟随 end 76 }