using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class MouseController : BaseController
{
///
/// 控制对象的Transform
/// 若为空则控制当前脚本所在对象
///
[Tooltip("控制对象的Transform,默认为当前对象")]
public Transform tran;
[Tooltip("允许使用鼠标右键控制旋转")]
public bool RotateEnabled = true;
[Tooltip("允许中键控制移动")]
public bool MoveEnabled = true;
[Tooltip("允许滚轮控制摄像机视场角")]
public bool CameraFovEnabled = true;
void Start()
{
if (tran == null)
tran = transform;
agent = GetComponent();
if (H5Controller.IsMobileBroswer())
{
Destroy(this);
}
}
void Update()
{
if (CanControl && !IsJoyStickOnControl)
{
if (RotateEnabled)
RotateWithMouse();
if (MoveEnabled)
MoveWithMidBtn();
if (CameraFovEnabled)
CameraFOVWithWheel();
}
}
#region 鼠标右键控制旋转
// 临时变量
float mouseX;
float mouseY;
float rotationX;
float rotationY;
Quaternion rotation;
[Header("鼠标右键控制参数")]
///
/// 允许控制的方向枚举
///
[Tooltip("允许控制的方向枚举")]
public RotationAxes axes = RotationAxes.MouseXAndY;
///
/// 向下旋转角度限制
///
[Tooltip("向下旋转角度限制")]
public int yRotationMinLimit = -10;
///
/// 向上旋转角度限制
///
[Tooltip("向上旋转角度限制")]
public int yRotationMaxLimit = 30;
///
/// 水平旋转速度
///
[Tooltip("水平旋转速度")]
public float xRotationSpeed = 3;
///
/// 垂直旋转速度
///
[Tooltip("垂直旋转速度")]
public float yRotationSpeed = 3;
///
/// 视角重置为水平的速度
///
[Tooltip("视角重置为水平的速度")]
public float resetAngleSpeed = 1f;
NavMeshAgent agent;
Vector3 upPos;
Vector3 vTmp;
void RotateWithMouse()
{
// 鼠标左键
if ((Input.touchCount == 0 && Input.GetMouseButton(0)) || Input.touchCount == 1)
{
// agent.enabled = false;
IsMouseOnControl = true;
if (axes == RotationAxes.MouseXAndY)
{
rotationX = CommonData.MainCamera.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * xRotationSpeed;
rotationY += Input.GetAxis("Mouse Y") * yRotationSpeed;
rotationY = Mathf.Clamp(rotationY, yRotationMinLimit, yRotationMaxLimit);
CommonData.MainCamera.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
CommonData.MainCamera.transform.Rotate(0, Input.GetAxis("Mouse X") * yRotationSpeed, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * yRotationSpeed;
rotationY = Mathf.Clamp(rotationY, yRotationMinLimit, yRotationMaxLimit);
CommonData.MainCamera.transform.localEulerAngles = new Vector3(-rotationY, CommonData.MainCamera.transform.localEulerAngles.y, 0);
}
//GetComponent().enabled = false;
}
if (Input.GetMouseButtonUp(0))
{
rotationX = 0;
rotationY = 0;
// 获取当前的摄像机视角旋转位置
upPos = tran.localRotation.eulerAngles;
if (upPos.x > yRotationMaxLimit + 1)
{
upPos.x -= 360f;
}
IsMouseOnControl = false;
}
if (IsMouseOnControl && agent != null && !agent.enabled)
{
vTmp = tran.localRotation.eulerAngles;
if (Mathf.Abs(vTmp.x) <= 0.01f)
{
vTmp.x = 0;
// agent.enabled = true;
IsMouseOnControl = false;
}
else
{
vTmp.x -= Time.deltaTime * upPos.x * resetAngleSpeed;
if ((upPos.x > 0 && vTmp.x <= 0) || (upPos.x < 0 && vTmp.x >= 360f))
{
vTmp.x = 0;
// agent.enabled = true;
IsMouseOnControl = false;
}
}
tran.localRotation = Quaternion.Euler(vTmp);
}
}
///
/// 控制旋转的角度
///
/// 角度值
/// 最小值
/// 最大值
float ClampValue(float value, float min, float max)
{
// 限定在0-360之间
if (value <= -360)
value += 360;
if (value >= 360)
value -= 360;
//限制value的值在min和max之间,如果value小于min,返回min。如果value大于max,返回max,否则返回value
return Mathf.Clamp(value, min, max);
}
#endregion
#region 中键控制移动
// 临时变量
float mouseX2;
float mouseY2;
Vector3 moveDir;
///
/// 中键移动速度
///
[Header("鼠标中键控制参数")]
[Tooltip("中键移动速度")]
public float moveSpeed = 1;
///
/// 中键控制移动
///
public void MoveWithMidBtn()
{
if (Input.GetMouseButton(2))
{
mouseX2 = Input.GetAxis("Mouse X");
mouseY2 = Input.GetAxis("Mouse Y");
if (mouseX2 != 0 || mouseY2 != 0)
{
IsMouseOnControl = true;
//相机位置的偏移量(Vector3类型,实现原理是:向量的加法)
moveDir = (mouseX2 * -tran.right + mouseY2 * -tran.forward);
//限制y轴的偏移量
moveDir.y = 0;
tran.position += moveDir * 0.5f * moveSpeed;
}
}
if (Input.GetMouseButtonUp(2))
{
IsMouseOnControl = false;
}
}
#endregion
#region 滚轮控制相机视角缩放
// 临时变量
float fov; // 角度
float wheel; // 滚轮偏移量
///
/// fov 最小角度
///
[Tooltip("fov 最小角度")]
public int fovMinLimit = 25;
///
/// fov 最大角度
///
[Tooltip("fov 最大角度")]
public int fovMaxLimit = 75;
///
/// fov 变化速度
///
[Tooltip("fov 变化速度")]
public float fovSpeed = 50.0f;
///
/// 滚轮控制相机视场角缩放
///
public void CameraFOVWithWheel()
{
wheel = Input.GetAxis("Mouse ScrollWheel");
if (wheel != 0)
{
fov = CommonData.MainCamera.fieldOfView;
//获取鼠标滚轮的滑动量
fov += wheel * Time.deltaTime * 100 * fovSpeed;
// fov 限制修正
fov = Mathf.Clamp(fov, fovMinLimit, fovMaxLimit);
//改变相机的 fov
CommonData.MainCamera.fieldOfView = fov;
}
}
#endregion
}
///
/// 旋转控制轴枚举
///
public enum RotationAxes
{
MouseXAndY,
MouseX,
MouseY
}