using UnityEngine;
using System.Collections;
///
public class CameraRotateAround : MonoBehaviour
{
public enum CameraState
{
///
/// 战斗默认
///
BattleDefault,
///
/// 战斗胜利
///
BattleWin,
///
/// 角色准备
///
Ready,
///
/// 相机修正(技能相机特效完毕后,同步主相机到最后帧的相机位置)
///
Correct,
///
/// 自由移动
///
Free
}
///
/// 是否打包模式
///
public bool IsAndroidAPK = false;
///
/// 主相机要围绕其旋转的物体
///
public Transform target;//
///
/// 主相机与目标物体之间的距离
///
public float distance = 7.0f;//
///*****************************************水平滚动相关
///
/// 主相机水平方向旋转速度
///
public float xSpeed = 70.0f;
///
/// 主相机纵向旋转速度
///
public float ySpeed = 70.0f;
///****************************************垂直滚动相关
///
/// 最大y(单位是角度)
///
public int yMaxLimit = 60;
///
/// 最小y(单位是角度)
///
public int yMinLimit = 10;
///
/// 旋转衰减
///
public float RotationDamping = 10.0f;
///
/// 位置衰减
///
public float PosDamping = 5.0f;
///
/// 高度
///
public float Height = 0f;
///
/// 当前相机状态
///
public CameraState mState = CameraState.Ready;
///
/// 当前方向
///
private Vector3 mWantedPosition = Vector3.zero;
///
/// 当前角度
///
private Quaternion mWantedRotation = Quaternion.identity;
///
///
///
private float eulerAngles_x;
///
///
///
private float eulerAngles_y;
///
///
///
public Touch touch;
///
/// 实例
///
public static CameraRotateAround Instance = null;
///
/// Awake
///
void Awake()
{
Instance = this;
}
///
/// Start
///
void Start()
{
ChangeState(mState);
}
///
/// 修改状态
///
///
public void ChangeState(CameraState _state)
{
ResetAngle();
switch (_state)
{
case CameraState.BattleDefault:
mWantedPosition = target.TransformPoint(-6.0f, 6.0f, 14.1f);
mWantedRotation = Quaternion.Euler(23.1f, 154.6f, 0);
Invoke("LateFree", 1f);
break;
case CameraState.BattleWin:
mWantedPosition = target.TransformPoint(0f, 1.14f, -1.48f);
mWantedRotation = Quaternion.Euler(-9.8f, 0, 0);
break;
case CameraState.Ready:
mWantedPosition = target.TransformPoint(0f, 3.0f, -2.0f);
mWantedRotation = Quaternion.Euler(10, 0, 0);
break;
case CameraState.Correct:
Invoke("LateFree", 1f);
break;
case CameraState.Free:
ResetAngle();
break;
}
mState = _state;
}
///
/// 延迟修改为自由状态
///
private void LateFree()
{
ChangeState(CameraState.Free);
}
///
/// 修正摄像机位置
///
public void Correct(Vector3 pos, Quaternion rotate)
{
mWantedPosition = target.TransformPoint(pos);
mWantedRotation = rotate;
ChangeState(CameraState.Correct);
}
///
///
///
private void ResetAngle()
{
///当前物体的欧拉角
Vector3 eulerAngles = this.transform.eulerAngles;
this.eulerAngles_x = eulerAngles.y;
this.eulerAngles_y = eulerAngles.x;
}
///
///
///
private void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
ChangeState(CameraState.BattleWin);
}
}
///
/// LateUpdate
///
private void LateUpdate()
{
if (this.target == null)
{
return;
}
if (mState != CameraState.Free)
{
//Quaternion mWantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp(transform.rotation, mWantedRotation, Time.deltaTime * RotationDamping);
transform.position = Vector3.Lerp(transform.position, mWantedPosition, Time.deltaTime * PosDamping);
return;
}
if (IsAndroidAPK)
{
if ((Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved))
{
touch = Input.GetTouch(0);
this.eulerAngles_x += ((touch.deltaPosition.x * this.xSpeed) * this.distance) * 0.02f;
this.eulerAngles_y -= (touch.deltaPosition.y * this.ySpeed) * 0.02f;
this.eulerAngles_y = ClampAngle(this.eulerAngles_y, (float)this.yMinLimit, (float)this.yMaxLimit);
Quaternion quaternion = Quaternion.Euler(this.eulerAngles_y, this.eulerAngles_x, (float)0);
Vector3 vector = ((Vector3)(quaternion * new Vector3((float)0, (float)0, -this.distance))) + this.target.position;
///更改主相机的旋转角度和位置
this.transform.rotation = quaternion;
this.transform.position = vector;
return;
}
}
else
{
if (Input.GetMouseButton(0))
{
this.eulerAngles_x += ((Input.GetAxis("Mouse X") * this.xSpeed) * this.distance) * 0.02f;
this.eulerAngles_y -= (Input.GetAxis("Mouse Y") * this.ySpeed) * 0.02f;
this.eulerAngles_y = ClampAngle(this.eulerAngles_y, (float)this.yMinLimit, (float)this.yMaxLimit);
Quaternion quaternion = Quaternion.Euler(this.eulerAngles_y, this.eulerAngles_x, (float)0);
Vector3 vector = ((Vector3)(quaternion * new Vector3((float)0, (float)0, -this.distance))) + this.target.position;
///更改主相机的旋转角度和位置
this.transform.rotation = quaternion;
this.transform.position = vector;
}
}
}
///
/// 把角度限制到给定范围内
///
///
///
///
///
private float ClampAngle(float angle, float min, float max)
{
while (angle < -360)
{
angle += 360;
}
while (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
}