写个枚举控制相机
public enum CameraType
{
Vertival,
Horizontal,
Normal
}
public CameraType cameraType;
public float dampTime = 1.5f;
public Transform target;
//相机移动速度,初始速度清零
private Vector3 velocity = Vector3.zero;
//相机单列
private static CameraCtrl instance;
public static CameraCtrl Instance
{
get
{
return instance;
}
}
//屏幕的默认宽高的1/100(预编译)
#if UNITY_ANDROID
private static float devHeight=8.54f;
private static float devWidth=4.8f;
#elif UNITY_IPHONE
private static float devHeight=9.6f
private static float devWidth=6.4f;
#else
private static float devHeight = 19.20f;
private static float devWidth = 10.80f;
#endif
void Awake()
{
instance = this;
//屏幕适配
float screenHeight = Screen.height;
float orthographicSize = this.GetComponent<Camera>().orthographicSize;
float aspectRatio = Screen.width * 1.0f / Screen.height;
float cameraWidth = orthographicSize * 2 * aspectRatio;
if (cameraWidth < devWidth)
{
orthographicSize = devWidth / (2 * aspectRatio);
Debug.Log("new orthographicSize=" + orthographicSize);
this.GetComponent<Camera>().orthographicSize = orthographicSize;
}
}
void LateUpdate()
{
if (target)
{
SetCamera();
}
{
SetTarget();
}
}
/// <summary>
/// 设置相机
/// </summary>
void SetCamera()
{
Vector3 point = GetComponent<Camera>().WorldToViewportPoint(target.position);
Vector3 delta = target.position - GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
Vector3 destination = transform.position + delta;
switch(cameraType)
{
case CameraType.Vertival: //竖直相机
transform.position = Vector3.SmoothDamp(transform.position, new Vector3(transform.position.x, destination.y, destination.z), ref velocity, dampTime);
break;
case CameraType.Horizontal: //水平相机
transform.position = Vector3.SmoothDamp(transform.position, new Vector3(destination.x, transform.position.y, destination.z), ref velocity, dampTime);
break;
case CameraType.Normal: //无限制相机
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
break;
default:
break;
}
}
/// <summary>
/// 设置目标
/// </summary>
void SetTarget()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
网友评论