136 lines
4.0 KiB
C#
136 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class CameraMove : CameraController
|
|
{
|
|
/// <summary>
|
|
/// 速度
|
|
/// </summary>
|
|
private float m_Speed;
|
|
/// <summary>
|
|
/// 加速度
|
|
/// </summary>
|
|
private float m_AcceSpeed = 50;
|
|
/// <summary>
|
|
/// 最大移动速度
|
|
/// </summary>
|
|
public float maxSpeed = 50;
|
|
|
|
[Range(0, 1), Tooltip("旋转响应速度")]
|
|
public float RotLerp = 0.4f;
|
|
|
|
|
|
[Range(0, 1), Tooltip("移动响应速度")]
|
|
public float AccelerationLerp = 0.3f;
|
|
|
|
/// <summary>
|
|
/// 滚轮移动速度
|
|
/// </summary>
|
|
private float m_ScrollSpeed = 80;
|
|
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
Debug.Log("相机控制初始化");
|
|
InitTransform();
|
|
}
|
|
|
|
bool lostFocus;
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
if (ShieldingOperation) return;
|
|
|
|
if (!Application.isFocused)
|
|
lostFocus = true;
|
|
|
|
#region 旋转
|
|
if (UtilityInput.HoldRightMouse)
|
|
{
|
|
//解决当重新获取焦点时,鼠标移动值异常的情况
|
|
if (lostFocus)
|
|
{
|
|
lostFocus = false;
|
|
return;
|
|
}
|
|
horizontal += UtilityInput.MouseMoveX;
|
|
vertical -= UtilityInput.MouseMoveY;
|
|
|
|
vertical = Mathf.Clamp(vertical, -90, 90);
|
|
}
|
|
eulerx = Mathf.Lerp(eulerx, horizontal, RotLerp);
|
|
eulery = Mathf.Lerp(eulery, vertical, RotLerp);
|
|
#endregion
|
|
|
|
#region 移动
|
|
//键盘移动
|
|
if (!Input.GetKey(KeyCode.LeftControl) && !Input.GetKey(KeyCode.RightControl))
|
|
{
|
|
if (UtilityInput.Horizontal != 0 || UtilityInput.Vertical != 0)
|
|
{
|
|
if (Target != null) Target = null;
|
|
if (m_Speed < maxSpeed)
|
|
m_Speed += Time.deltaTime * m_AcceSpeed;
|
|
myPos += transform.TransformDirection(Dirction);
|
|
}
|
|
else
|
|
{
|
|
m_Speed -= Time.deltaTime * m_AcceSpeed;
|
|
}
|
|
m_Speed = Mathf.Clamp(m_Speed, 0, maxSpeed);
|
|
}
|
|
|
|
//滚轮前进
|
|
//if (Dirction.Equals(Vector3.zero) && UtilityInput.Scroll != 0)
|
|
//{
|
|
// if (Target != null) distance -= UtilityInput.Scroll * m_ScrollSpeed;
|
|
// distance = Mathf.Clamp(distance, 20, 500);
|
|
// myPos += transform.TransformDirection(Vector3.forward * UtilityInput.Scroll * m_ScrollSpeed);
|
|
//}
|
|
|
|
//滚轮移动
|
|
if (Dirction.Equals(Vector3.zero) && UtilityInput.HoldMiddleMouse)
|
|
{
|
|
float x = UtilityInput.MouseMoveX;
|
|
float y = UtilityInput.MouseMoveY;
|
|
myPos += transform.TransformDirection(new Vector3(-x, -y) * 2);
|
|
}
|
|
|
|
#endregion
|
|
|
|
transform.rotation = Quaternion.Euler(eulery, eulerx, 0);
|
|
if (Target == null)
|
|
{
|
|
myPos.x = Mathf.Clamp(myPos.x, XLimit.x, XLimit.y);
|
|
myPos.y = Mathf.Clamp(myPos.y, YLimit.x, YLimit.y);
|
|
myPos.z = Mathf.Clamp(myPos.z, ZLimit.x, ZLimit.y);
|
|
//transform.position = Vector3.Lerp(transform.position, myPos, AccelerationLerp);
|
|
transform.localPosition = Vector3.Lerp(transform.localPosition, myPos, AccelerationLerp);
|
|
}
|
|
else
|
|
{
|
|
//存在定位目标时,绕目标旋转
|
|
Vector3 tempPos = transform.rotation * Vector3.back * Distance + Target.position;
|
|
tempPos.x = Mathf.Clamp(tempPos.x, XLimit.x, XLimit.y);
|
|
tempPos.y = Mathf.Clamp(tempPos.y, YLimit.x, YLimit.y);
|
|
tempPos.z = Mathf.Clamp(tempPos.z, ZLimit.x, ZLimit.y);
|
|
transform.position = myPos = tempPos;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 键盘操作
|
|
/// </summary>
|
|
public Vector3 Dirction
|
|
{
|
|
get
|
|
{
|
|
Vector3 tmp= new Vector3(UtilityInput.Horizontal, 0, UtilityInput.Vertical) * Time.deltaTime * m_Speed * (UtilityInput.SpeedUp ? 2 : 1);
|
|
//Debug.Log(tmp);
|
|
return tmp;
|
|
}
|
|
|
|
}
|
|
}
|