using System;
using System.Linq;
using UnityEngine;
using UnityEngine.PlayerLoop;
public class CameraRotate : MonoBehaviour
{
private CameraController cameraController;
private void Start()
{
cameraController = UITools.GetOrCreateComponent<CameraController>(this.gameObject);
}
public void UpdateDo()
{
CheckOpenCameraRotate();
RotateCamera();
}
//------------------------检测是否开启相机旋转---------------------------------
private string cameraFightAnimName = "Fight";
private int cameraFightAnimHash;
private Animator cameraAnimator;
private bool checkCameraRotate;
public void SetCheckCameraRotate(bool check)
{
checkCameraRotate = check;
if (checkCameraRotate)
{
GameObject sceneRoot = GameObject.Find("SceneRoot");
if (sceneRoot != null)
{
cameraAnimator = sceneRoot.transform.GetComponent<Animator>();
cameraFightAnimHash = Animator.StringToHash(this.cameraFightAnimName);
}
}
else
{
cameraAnimator = null;
}
}
void CheckOpenCameraRotate()
{
if (!checkCameraRotate) return;
if (cameraAnimator!=null && this.cameraAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash==cameraFightAnimHash)
{
checkCameraRotate = false;
OpenRotateCamera(true);
}
}
//-----------------------------------------------------
//------用户滑动屏幕,相机围绕中心点旋转----------
public Transform centerTrs;
private bool canRotateCamera;
[SerializeField]
private float rotateSpeed=1;
[SerializeField]
private float limitEulerAnglesX = 15f;
[SerializeField]
private float limitEulerAnglesY = 15f;
public Vector3 sceneCenterPoint;
private float maxEulerAnglesX;
private float minEulerAnglesX;
private float maxEulerAnglesY;
private float minEulerAnglesY;
private Vector3 originCameraPos;
private Vector3 originCameraAngles;
public Vector3 lastCameraPos;
public Vector3 lastCameraAngles;
public void InitRecordCameraCfg()
{
originCameraPos = this.transform.position;
originCameraAngles.x = this.transform.eulerAngles.x;
originCameraAngles.y = this.transform.eulerAngles.y;
lastCameraPos = this.transform.position;
lastCameraAngles = this.transform.eulerAngles;
}
public void OpenRotateCamera(bool open)
{
InitRecordCameraCfg();
cameraController.IsUpdateCameraInBindTrans = !open;
CanRotateCamera(open);
AngleLimit();
GameObject go = GameObject.Find("Cam_Pos");
if (go != null)
{
sceneCenterPoint = go.transform.position;
}
else
{
sceneCenterPoint = new Vector3(8.93f,0,5.11f);
}
}
public void CanRotateCamera(bool can)
{
canRotateCamera = can;
}
void RotateCamera()
{
if (Input.GetMouseButton(0)&&canRotateCamera&&!cameraController.IsUpdateCameraInBindTrans)
{
RotateAround();
}
}
void RotateAround()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
//左右旋转
float rotateAngleY = mouseX * rotateSpeed;
transform.RotateAround(sceneCenterPoint, Vector3.up, rotateAngleY);
float hadRotateAngleY = transform.eulerAngles.y + rotateAngleY;
if (rotateAngleY>0 &&
hadRotateAngleY<180 &&
hadRotateAngleY > maxEulerAnglesY)
{
transform.RotateAround(sceneCenterPoint, Vector3.up, rotateAngleY-(hadRotateAngleY-maxEulerAnglesY));
}
else if(rotateAngleY<0 &&
hadRotateAngleY>180 &&
hadRotateAngleY< minEulerAnglesY)
{
transform.RotateAround(sceneCenterPoint,Vector3.up,rotateAngleY+(minEulerAnglesY-hadRotateAngleY));
}
//垂直旋转
float rotateAngleX = mouseY * rotateSpeed;
transform.RotateAround(sceneCenterPoint, transform.right, rotateAngleX);
float hadRotateAngleX = transform.eulerAngles.x + rotateAngleX;
if (rotateAngleX > 0 &&
maxEulerAnglesX <= 90 &&
hadRotateAngleX < 180 &&
hadRotateAngleX >= maxEulerAnglesX)
{
transform.RotateAround(sceneCenterPoint,transform.right,rotateAngleX-(hadRotateAngleX - maxEulerAnglesX));
}
else if (rotateAngleX > 0 &&
maxEulerAnglesX <= 90 &&
transform.eulerAngles.z>=179 &&
hadRotateAngleX <= maxEulerAnglesX)
{
transform.RotateAround(sceneCenterPoint,transform.right,rotateAngleX-(maxEulerAnglesX - hadRotateAngleX));
}
else if (rotateAngleX > 0 &&
maxEulerAnglesX > 90 &&
transform.eulerAngles.z>=179 &&
hadRotateAngleX < 180-maxEulerAnglesX)
{
transform.RotateAround(sceneCenterPoint,transform.right,rotateAngleX-( (180-maxEulerAnglesX)-hadRotateAngleX));
}
else if (rotateAngleX < 0 &&
hadRotateAngleX < 180 &&
minEulerAnglesX < 180 &&
hadRotateAngleX < minEulerAnglesX)
{
transform.RotateAround(sceneCenterPoint,transform.right,rotateAngleX+(minEulerAnglesX-hadRotateAngleX));
}
else if (rotateAngleX < 0 &&
hadRotateAngleX>180 &&
hadRotateAngleX <360- minEulerAnglesX)
{
transform.RotateAround(sceneCenterPoint,transform.right,rotateAngleX+((360-minEulerAnglesX)-hadRotateAngleX));
}
else if (rotateAngleX < 0 &&
hadRotateAngleX > 180 &&
hadRotateAngleX < minEulerAnglesX)
{
transform.RotateAround(sceneCenterPoint,transform.right,rotateAngleX-(hadRotateAngleX-minEulerAnglesX));
}
lastCameraPos = this.transform.position;
lastCameraAngles = this.transform.eulerAngles;
}
void AngleLimit()
{
maxEulerAnglesY = originCameraAngles.y + limitEulerAnglesY;
if (maxEulerAnglesY >= 360)
{
maxEulerAnglesY = maxEulerAnglesY- 360;
}
else if (maxEulerAnglesY<0)
{
maxEulerAnglesY = 360 + maxEulerAnglesY;
}
minEulerAnglesY = originCameraAngles.y - limitEulerAnglesY;
if (minEulerAnglesY >= 360)
{
minEulerAnglesY = minEulerAnglesY- 360;
}
else if (minEulerAnglesY<0)
{
minEulerAnglesY = 360 + minEulerAnglesY;
}
maxEulerAnglesX = originCameraAngles.x+ limitEulerAnglesX;
if (maxEulerAnglesX >= 360)
{
maxEulerAnglesX = maxEulerAnglesX- 360;
}
else if (maxEulerAnglesX<0)
{
maxEulerAnglesX = 360 + maxEulerAnglesX;
}
minEulerAnglesX = originCameraAngles.x - limitEulerAnglesX;
if (minEulerAnglesX >= 360)
{
minEulerAnglesX = minEulerAnglesX- 360;
}
else if (minEulerAnglesX<0)
{
minEulerAnglesX = 360 + minEulerAnglesX;
}
}
//----------------------------------------------------------------------------------------
TweenRotation tr;
public void TweenRotation(Vector3 to,float duration,float delay=0)
{
tr = UITools.GetOrCreateComponent<TweenRotation>(this.gameObject);
//tr.delay = delay;
tr.method = UITweener.Method.Linear;
tr.@from = transform.eulerAngles;
tr.to = to;
tr.duration = duration;
}
}
|