using DG.Tweening;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace KOL
{
public class PlayerMoveControl : MonoBehaviour
{
private NavMeshAgent agent;
private CharacterController controller;
private GameObject Ring;
private Material ringM;
private Color ringColor;
private Tween tween;
//public enum MouseState
//{
// None,
// MidMouseBtn,
// LeftMouseBtn
//}
public float moveSpeed = 2.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
//public MouseState mMouseState = MouseState.None;
public Camera mCamera;
#region Camera Rotation
//旋转最大角度
public int yRotationMinLimit = -20;
public int yRotationMaxLimit = 80;
//旋转速度
public float xRotationSpeed = 250.0f;
public float yRotationSpeed = 120.0f;
//旋转角度
public float xRotation = 0.0f;
public float yRotation = 0.0f;
public bool lockRotate = false;
public bool isMove = false;
public Vector2 target;
private void OnEnable()
{
SetEuler();
}
private void Awake()
{
if (mCamera == null)
{
Debug.LogError(GetType() + "camera Get Error ……");
}
controller = GetComponent<CharacterController>();
agent = GetComponent<NavMeshAgent>();
Ring = Instantiate(Resources.Load<GameObject>("Plane"));
Ring.gameObject.SetActive(false);
ringM = Ring.transform.GetComponentInChildren<MeshRenderer>().material;
ringColor = ringM.color;
//GetDefaultFov();
}
void Update()
{
if (agent==null)
{
return;
}
if (Input.GetMouseButtonDown(0))
{
//射线检测
Ray ray = mCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
if (!EventSystem.current.IsPointerOverGameObject()&&isCollider && hit.transform.CompareTag("Plane"))
{
agent.SetDestination(hit.point);
Ring.gameObject.SetActive(true);
Ring.transform.position = hit.point;
target=new Vector2(hit.point.x, hit.point.z);
ringM.color = ringColor;
if (tween!=null)
{
tween.Kill();
tween = null;
}
tween = ringM.DOFade(0, 1.5f).SetEase(Ease.OutQuart) ;
isMove = true;
}
}
if (isMove)
{
if (Vector2.Distance(target, new Vector2(transform.position.x, transform.position.z))< 0.05f)
{
SetEuler();
lockRotate = false;
isMove = false;
print("到达终点");
}
else
{
lockRotate = true;
}
}
//PlayerMove();
}
private void LateUpdate()
{
if (Input.GetKey(KeyCode.Mouse1)&&!lockRotate)
{
CameraRotate();
//CameraFOV();
//CameraMove();
}
}
/// <summary>
/// 鼠标移动进行旋转
/// </summary>
void CameraRotate()
{
//if (mMouseState == MouseState.None)
{
//Input.GetAxis("MouseX")获取鼠标移动的X轴的距离
xRotation -= Input.GetAxis("Mouse X") * xRotationSpeed * 0.02f;
yRotation += Input.GetAxis("Mouse Y") * yRotationSpeed * 0.02f;
yRotation = ClampValue(yRotation, yRotationMinLimit, yRotationMaxLimit);//这个函数在结尾
Quaternion rotationPlayer = Quaternion.Euler(0, -xRotation, 0);//欧拉角转化为四元数
Quaternion rotationCamera = Quaternion.Euler(-yRotation, 0, 0);
transform.localRotation = rotationPlayer;
mCamera.transform.localRotation = rotationCamera;
}
}
public void SetEuler()
{
xRotation = -transform.localEulerAngles.y;
yRotation = -transform.localEulerAngles.x;
}
#endregion
#region
void PlayerMove()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
#endregion
#region Camera fov
fov 最大最小角度
//public int fovMinLimit = 25;
//public int fovMaxLimit = 75;
fov 变化速度
//public float fovSpeed = 50.0f;
fov 角度
//private float fov = 0.0f;
//void GetDefaultFov()
//{
// fov = mCamera.fieldOfView;
//}
/// <summary>
/// 滚轮控制相机视角缩放
/// </summary>
//public void CameraFOV()
//{
// //获取鼠标滚轮的滑动量
// fov += Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 100 * fovSpeed;
// // fov 限制修正
// fov = ClampValue(fov, fovMinLimit, fovMaxLimit);
// //改变相机的 fov
// mCamera.fieldOfView = (fov);
//}
//#endregion
//#region Camera Move
//float _mouseX = 0;
//float _mouseY = 0;
//public float moveSpeed = 1;
/ <summary>
/ 中键控制拖动
/ </summary>
//public void CameraMove()
//{
// if (Input.GetMouseButton(2))
// {
// _mouseX = Input.GetAxis("Mouse X");
// _mouseY = Input.GetAxis("Mouse Y");
// //相机位置的偏移量(Vector3类型,实现原理是:向量的加法)
// Vector3 moveDir = (_mouseX * -transform.right + _mouseY * -transform.forward);
// //限制y轴的偏移量
// moveDir.y = 0;
// transform.position += moveDir * 0.5f * moveSpeed;
// }
// else if (Input.GetMouseButtonDown(2))
// {
// mMouseState = MouseState.MidMouseBtn;
// Debug.Log(GetType() + "mMouseState = " + mMouseState.ToString());
// }
// else if (Input.GetMouseButtonUp(2))
// {
// mMouseState = MouseState.None;
// Debug.Log(GetType() + "mMouseState = " + mMouseState.ToString());
// }
//}
#endregion
#region tools ClampValue
//值范围值限定
float ClampValue(float value, float min, float max)//控制旋转的角度
{
if (value < -360)
value += 360;
if (value > 360)
value -= 360;
return Mathf.Clamp(value, min, max);//限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
}
#endregion
}
}