271 lines
7.1 KiB
C#
271 lines
7.1 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.AI;
|
||
|
||
public class MouseController : BaseController
|
||
{
|
||
/// <summary>
|
||
/// 控制对象的Transform
|
||
/// 若为空则控制当前脚本所在对象
|
||
/// </summary>
|
||
[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<NavMeshAgent>();
|
||
|
||
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("鼠标右键控制参数")]
|
||
/// <summary>
|
||
/// 允许控制的方向枚举
|
||
/// </summary>
|
||
[Tooltip("允许控制的方向枚举")]
|
||
public RotationAxes axes = RotationAxes.MouseXAndY;
|
||
/// <summary>
|
||
/// 向下旋转角度限制
|
||
/// </summary>
|
||
[Tooltip("向下旋转角度限制")]
|
||
public int yRotationMinLimit = -10;
|
||
/// <summary>
|
||
/// 向上旋转角度限制
|
||
/// </summary>
|
||
[Tooltip("向上旋转角度限制")]
|
||
public int yRotationMaxLimit = 30;
|
||
/// <summary>
|
||
/// 水平旋转速度
|
||
/// </summary>
|
||
[Tooltip("水平旋转速度")]
|
||
public float xRotationSpeed = 3;
|
||
/// <summary>
|
||
/// 垂直旋转速度
|
||
/// </summary>
|
||
[Tooltip("垂直旋转速度")]
|
||
public float yRotationSpeed = 3;
|
||
|
||
/// <summary>
|
||
/// 视角重置为水平的速度
|
||
/// </summary>
|
||
[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<NavMeshAgent>().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);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 控制旋转的角度
|
||
/// </summary>
|
||
/// <param name="value">角度值</param>
|
||
/// <param name="min">最小值</param>
|
||
/// <param name="max">最大值</param>
|
||
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;
|
||
|
||
/// <summary>
|
||
/// 中键移动速度
|
||
/// </summary>
|
||
[Header("鼠标中键控制参数")]
|
||
[Tooltip("中键移动速度")]
|
||
public float moveSpeed = 1;
|
||
|
||
/// <summary>
|
||
/// 中键控制移动
|
||
/// </summary>
|
||
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; // 滚轮偏移量
|
||
|
||
/// <summary>
|
||
/// fov 最小角度
|
||
/// </summary>
|
||
[Tooltip("fov 最小角度")]
|
||
public int fovMinLimit = 25;
|
||
/// <summary>
|
||
/// fov 最大角度
|
||
/// </summary>
|
||
[Tooltip("fov 最大角度")]
|
||
public int fovMaxLimit = 75;
|
||
/// <summary>
|
||
/// fov 变化速度
|
||
/// </summary>
|
||
[Tooltip("fov 变化速度")]
|
||
public float fovSpeed = 50.0f;
|
||
|
||
/// <summary>
|
||
/// 滚轮控制相机视场角缩放
|
||
/// </summary>
|
||
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
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 旋转控制轴枚举
|
||
/// </summary>
|
||
public enum RotationAxes
|
||
{
|
||
MouseXAndY,
|
||
MouseX,
|
||
MouseY
|
||
} |