123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using UnityEngine;
- using System.Collections;
- ///
- public class CameraRotateAround : MonoBehaviour
- {
- public enum CameraState
- {
- /// <summary>
- /// 战斗默认
- /// </summary>
- BattleDefault,
- /// <summary>
- /// 战斗胜利
- /// </summary>
- BattleWin,
- /// <summary>
- /// 角色准备
- /// </summary>
- Ready,
- /// <summary>
- /// 相机修正(技能相机特效完毕后,同步主相机到最后帧的相机位置)
- /// </summary>
- Correct,
- /// <summary>
- /// 自由移动
- /// </summary>
- Free
- }
- /// <summary>
- /// 是否打包模式
- /// </summary>
- public bool IsAndroidAPK = false;
- /// <summary>
- /// 主相机要围绕其旋转的物体
- /// </summary>
- public Transform target;//
- /// <summary>
- /// 主相机与目标物体之间的距离
- /// </summary>
- public float distance = 7.0f;//
- ///*****************************************水平滚动相关
- /// <summary>
- /// 主相机水平方向旋转速度
- /// </summary>
- public float xSpeed = 70.0f;
- /// <summary>
- /// 主相机纵向旋转速度
- /// </summary>
- public float ySpeed = 70.0f;
- ///****************************************垂直滚动相关
- /// <summary>
- /// 最大y(单位是角度)
- /// </summary>
- public int yMaxLimit = 60;
- /// <summary>
- /// 最小y(单位是角度)
- /// </summary>
- public int yMinLimit = 10;
- /// <summary>
- /// 旋转衰减
- /// </summary>
- public float RotationDamping = 10.0f;
- /// <summary>
- /// 位置衰减
- /// </summary>
- public float PosDamping = 5.0f;
- /// <summary>
- /// 高度
- /// </summary>
- public float Height = 0f;
- /// <summary>
- /// 当前相机状态
- /// </summary>
- public CameraState mState = CameraState.Ready;
- /// <summary>
- /// 当前方向
- /// </summary>
- private Vector3 mWantedPosition = Vector3.zero;
- /// <summary>
- /// 当前角度
- /// </summary>
- private Quaternion mWantedRotation = Quaternion.identity;
- /// <summary>
- ///
- /// </summary>
- private float eulerAngles_x;
- /// <summary>
- ///
- /// </summary>
- private float eulerAngles_y;
- /// <summary>
- ///
- /// </summary>
- public Touch touch;
- /// <summary>
- /// 实例
- /// </summary>
- public static CameraRotateAround Instance = null;
- /// <summary>
- /// Awake
- /// </summary>
- void Awake()
- {
- Instance = this;
- }
- /// <summary>
- /// Start
- /// </summary>
- void Start()
- {
- ChangeState(mState);
- }
- /// <summary>
- /// 修改状态
- /// </summary>
- /// <param name="_state"></param>
- 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;
- }
- /// <summary>
- /// 延迟修改为自由状态
- /// </summary>
- private void LateFree()
- {
- ChangeState(CameraState.Free);
- }
- /// <summary>
- /// 修正摄像机位置
- /// </summary>
- public void Correct(Vector3 pos, Quaternion rotate)
- {
- mWantedPosition = target.TransformPoint(pos);
- mWantedRotation = rotate;
- ChangeState(CameraState.Correct);
- }
- /// <summary>
- ///
- /// </summary>
- private void ResetAngle()
- {
- ///当前物体的欧拉角
- Vector3 eulerAngles = this.transform.eulerAngles;
- this.eulerAngles_x = eulerAngles.y;
- this.eulerAngles_y = eulerAngles.x;
- }
- /// <summary>
- ///
- /// </summary>
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.W))
- {
- ChangeState(CameraState.BattleWin);
- }
- }
- /// <summary>
- /// LateUpdate
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// 把角度限制到给定范围内
- /// </summary>
- /// <param name="angle"></param>
- /// <param name="min"></param>
- /// <param name="max"></param>
- /// <returns></returns>
- 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);
- }
- }
|