照相机看跟踪功能,区分自由视角与缓动视角,视角可旋转、平移、拉远拉近
/// <summary>
/// 自由视角
/// </summary>
/// <param name="isSlowAction">是否缓动判断</param>
void FreeAngle(bool isSlowAction) {
#region 旋转
if (Input.GetMouseButton(0))
{
xCurrentAngle += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
yCurrentAngle -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
yCurrentAngle = ClampAngle(yCurrentAngle, yMinLimit, yMaxLimit);//y旋转角度限制
}
Quaternion rotationTo = Quaternion.Euler(yCurrentAngle, xCurrentAngle, 0);
#endregion
#region 缩放
if (Input.GetAxis("Mouse ScrollWheel") != 0.0f )
{
zCurrentScale = Input.GetAxis("Mouse ScrollWheel") * scaleSpeed;
}
#endregion
#region 平移
if(Input.GetMouseButton(1)) {
xCurrentScale = -Input.GetAxis("Mouse X") * Time.deltaTime * xMoveSpeed;
yCurrentScale = -Input.GetAxis("Mouse Y") * Time.deltaTime * yMoveSpeed;
xCurrentScale = xCurrentScale > 1 ? 1 : xCurrentScale;
yCurrentScale = yCurrentScale > 1 ? 1 : yCurrentScale;
xCurrentScale = xCurrentScale < -1 ? -1 : xCurrentScale;
yCurrentScale = yCurrentScale < -1 ? -1 : yCurrentScale;
}
#endregion
if(isSlowAction)//带缓动效果
{
m_Transform.rotation = Quaternion.Lerp(m_Transform.rotation, rotationTo, Time.deltaTime * lerpSpeed);
m_Transform.position = Vector3.Lerp(m_Transform.position, m_Transform.position + m_Transform.forward * (zCurrentScale > 0 ? ((zCurrentScale -= Time.deltaTime * slowlySpeed) < 0 ? zCurrentScale = 0 : zCurrentScale) : ((zCurrentScale += Time.deltaTime * slowlySpeed) > 0 ? zCurrentScale = 0 : zCurrentScale)) + m_Transform.right * (xCurrentScale > 0 ? ((xCurrentScale -= Time.deltaTime * slowlySpeed) < 0 ? xCurrentScale = 0 : xCurrentScale) : ((xCurrentScale += Time.deltaTime * slowlySpeed) > 0 ? xCurrentScale = 0 : xCurrentScale)) + m_Transform.up * (yCurrentScale > 0 ? ((yCurrentScale -= Time.deltaTime * slowlySpeed) < 0 ? yCurrentScale = 0 : yCurrentScale) : ((yCurrentScale += Time.deltaTime * slowlySpeed) > 0 ? yCurrentScale = 0 : yCurrentScale)), Time.deltaTime * lerpSpeed);
}
else//正常
{
Vector3 positionTo = rotationTo * new Vector3(xCurrentScale, yCurrentScale, zCurrentScale) + m_Transform.position;
xCurrentScale = 0;
yCurrentScale = 0;
zCurrentScale = 0;
m_Transform.rotation = rotationTo;
m_Transform.position = positionTo;
}
m_Transform.position = new Vector3(Mathf.Clamp(m_Transform.position.x, xMinRangeLimit, xMaxRangeLimit), Mathf.Clamp(m_Transform.position.y, yMinRangeLimit, yMaxRangeLimit), Mathf.Clamp(m_Transform.position.z, zMinRangeLimit, zMaxRangeLimit));//自由模式区域限制
}
/// <summary>
/// 跟踪视角
/// </summary>
/// <param name="isSlowAction">是否缓动判断</param>
void FollowAngle(bool isSlowAction) {
#if UNITY_WEBPLAYER
Debug.Log("web");
if(Input.GetMouseButton(0)) {
xCurrentAngle += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
yCurrentAngle -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
yCurrentAngle = ClampAngle(yCurrentAngle, yMinLimit, yMaxLimit);//y旋转角度限制
}
#endif
#if UNITY_STANDALONE_WIN
//Debug.Log("pc");
if (Input.GetMouseButton(0))
{
xCurrentAngle += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
yCurrentAngle -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
yCurrentAngle = ClampAngle(yCurrentAngle, yMinLimit, yMaxLimit);//y旋转角度限制
}
#endif
if (Input.GetMouseButton(1) )
{
xCurrentScale = -Input.GetAxis("Mouse X") * Time.deltaTime * xMoveSpeed;
yCurrentScale = -Input.GetAxis("Mouse Y") * Time.deltaTime * yMoveSpeed;
x += xCurrentScale;
y += yCurrentScale;
}
distance -= Input.GetAxis("Mouse ScrollWheel") * scaleSpeed;
distance = Mathf.Clamp(distance, minDistanceToTarget, maxDistanceToTarget);//缩放距离限制
Quaternion rotationTo = Quaternion.Euler(yCurrentAngle, xCurrentAngle, 0);
Vector3 positionTo = rotationTo * new Vector3(x, y, -distance) + target.position;
if(isSlowAction)//带缓动效果
{
m_Transform.rotation = Quaternion.Lerp(m_Transform.rotation, rotationTo, Time.deltaTime * lerpSpeed);
m_Transform.position = Vector3.Lerp(m_Transform.position, positionTo, Time.deltaTime * lerpSpeed);
}
else//正常
{
m_Transform.rotation = rotationTo;
m_Transform.position = positionTo;
}
}
/// <summary>
/// 限制视角旋转角度
/// </summary>
/// <param name="angle">当前角度</param>
/// <param name="min">最小旋转角度</param>
/// <param name="max">最大旋转角度</param>
/// <returns></returns>
float ClampAngle(float angle, float min, float max) {
if(angle < -360)
angle += 360;
if(angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}