一个游戏中可能会有各种类型的镜头,例如有时候是第一人称,有时是第三人称,有时又会给个特写等等,因此可以定义一个镜头类型枚举,在不同的场合进行切换,管理起来很方便。
CameraManager.cs
using UnityEngine;
using System.Collections; //镜头类型
public enum CameraType
{
NotFollow, //不跟随(可以*设置镜头)
FixedFollow, //固定跟随
SmoothFollow, //平滑跟随
} public class CameraManager : MonoSingletion<CameraManager> { private Camera camera;
private Transform cameraTra;
private Transform targetTra;
private CameraType cameraType; //固定跟随参数
private Vector3 fixedPosOffset;//位置偏移 //设置镜头
public void SetCamera(Camera camera)
{
this.camera = camera;
cameraTra = camera.gameObject.transform;
} //设置目标
public void SetTarget(GameObject go)
{
targetTra = go.transform;
} private void LateUpdate()
{
if ((cameraTra == null) || (targetTra == null))
{
return;
} switch (cameraType)
{
case CameraType.NotFollow:
return;
case CameraType.FixedFollow:
FixedFollow();
break;
case CameraType.SmoothFollow:
break;
default:
break;
}
} //-----------------------------------------------不跟随 start
public void SetNotFollow()
{
cameraType = CameraType.NotFollow;
}
//-----------------------------------------------不跟随 end //-----------------------------------------------固定跟随 start
public void SetFixedFollow(Vector3 fixedPosOffset, Vector3 rot)
{
cameraType = CameraType.FixedFollow;
this.fixedPosOffset = fixedPosOffset;
cameraTra.localEulerAngles = rot;
} private void FixedFollow()
{
cameraTra.position = targetTra.position + fixedPosOffset;
}
//-----------------------------------------------固定跟随 end
}