using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using ZXKFramework; public class DragRotationRight : MonoBehaviour //这个脚本挂在父物体上 { public float rotationSpeed = 2.5f; public float maxYRotation = 90.0f; public float minYRotation = -90.0f; private bool isRightMouseButtonHeld = false; // 用于跟踪右键是否按下 private float currentYRotationY; float newYRotation; Quaternion rotation; private Transform target; //目标物体 private Transform CarmerMain; //主相机 private Transform CarObj; //汽车 private Transform CarParent; //汽车的父物体 public bool isClose = false; private float wheel; private float computeR; private Vector3 computeEnd; [Header("滑轮移动的平滑速度")] public float zoomDampening = 2.5f; [Header("滑轮的最大距离")] public float MaxR = 0f; [Header("滑轮的最小距离")] public float MinR = 0; public float GetMaxR = 0; public float GetMinR = 0; private Vector3 CarStartNor; private Vector3 forsss; GameModel gameModel; void Start() { forsss = transform.right; gameModel = MVC.GetModel(); currentYRotationY = transform.eulerAngles.x; CarmerMain = Camera.main.transform; CarmerMain.transform.position = new Vector3(0, 0, -7); CarObj = transform; CarParent = transform.parent; computeR = -7; MinR = -8; //MaxR = Vector3.Distance(CarmerMain.position, transform.position); //最大距离= 最小距离+Vector3.zero到 物体位置 MaxR = 6; //最大距离= 最小距离+Vector3.zero到 物体位置 GetMaxR = MaxR; GetMinR = MinR; CarStartNor = transform.up; } //设置最小距离 //public void SetMinRDistance() //{ // GetMaxR = Vector3.Distance(CarmerMain.transform.position, transform.position) + 20; // GetMinR = GetMaxR - (Mathf.Abs(MinR) + MaxR); // computeR = -10; //} void Update() { //if (!EventSystem.current.IsPointerOverGameObject()) //{ if (!isClose) { if (gameModel.isMouseMove) { DragMove(); } else { isRightMouseButtonHeld = false; } DragRotate(); mouseScrollWheel(); } //} //else //{ // isRightMouseButtonHeld = false; //} } /// /// 检测是否点击UI /// /// /// public static bool IsPointerOverGameObject(Vector2 mousePosition) { //创建一个点击事件 PointerEventData eventData = new PointerEventData(EventSystem.current); eventData.position = mousePosition; List raycastResults = new List(); //向点击位置发射一条射线,检测是否点击UI EventSystem.current.RaycastAll(eventData, raycastResults); if (raycastResults.Count > 0) { if (raycastResults[0].gameObject.tag == "Other") { return true; } else { return false; } } return false; } /// /// 拖拽移动 /// void DragMove() { if (Input.GetMouseButtonDown(0)) { if (!EventSystem.current.IsPointerOverGameObject()) { isRightMouseButtonHeld = true; } } else if (Input.GetMouseButtonUp(0)) { isRightMouseButtonHeld = false; } if (isRightMouseButtonHeld) { if (!IsMouseInsideScreen()) return; // 获取水平轴(通常是鼠标X轴)和垂直轴(通常是鼠标Y轴)的输入 float moveX = Input.GetAxis("Mouse X"); float moveY = Input.GetAxis("Mouse Y"); Vector3 fwd = CarmerMain.forward; Vector3 vaxis = Vector3.Cross(fwd, Vector3.right); Vector3 haxis = Vector3.Cross(fwd, Vector3.up); transform.Translate(new Vector3(moveX, moveY, 0) * 15 * Time.deltaTime, Space.World); } } /// /// 拖拽旋转 /// void DragRotate() { if (Input.GetMouseButton(1)) { float y = Input.GetAxis("Mouse Y") * rotationSpeed; ////Debug.Log("开始" + currentYRotationY); //// 计算新的Y轴旋转角度,但限制在指定的范围内 //newYRotation = Mathf.Clamp(currentYRotationY + y, minYRotation, maxYRotation); //// 创建一个绕Y轴旋转的Quaternion //rotation = Quaternion.Euler(newYRotation - currentYRotationY, 0, 0); //// 将旋转应用到物体上 //transform.rotation = rotation * transform.rotation; //// 更新当前Y轴旋转角度 //currentYRotationY = newYRotation; float x = -Input.GetAxis("Mouse X") * rotationSpeed; transform.Rotate(transform.up, x, Space.World); transform.Rotate(forsss, -y, Space.World); } } void mouseScrollWheel() { if (Input.GetAxis("Mouse ScrollWheel") < 0 || Input.GetAxis("Mouse ScrollWheel") > 0) { wheel = Input.GetAxis("Mouse ScrollWheel"); if (wheel != 0) { computeR += wheel * zoomDampening; computeR = Mathf.Clamp(computeR, GetMinR, GetMaxR); computeEnd = Vector3.forward * computeR; CarmerMain.position = computeEnd; } } } /// /// 重置旋转 /// public void ResetRotation() { newYRotation = 0; currentYRotationY = 0; rotation = transform.rotation; } /// /// 目标点结束后更新角度 /// public void SetCurrentYRotationY() { Vector3 v3 = Vector3.ProjectOnPlane(transform.up, Vector3.right); Debug.Log("计算后的角度:" + Vector3.Angle(CarStartNor, v3)); Vector3 v1 = CarObj.transform.localEulerAngles; if (v3.z > 0) { currentYRotationY = Vector3.Angle(CarStartNor, v3); } else { currentYRotationY = -Vector3.Angle(CarStartNor, v3); } } float NormalizeAngle(float angle) { if (angle < -90) { angle += 360; } else if (angle > 90) { angle -= 360; } return angle; } private bool IsMouseInsideScreen() { Vector3 mousePos = Input.mousePosition; return mousePos.x >= 0 && mousePos.x < Screen.width && mousePos.y >= 0 && mousePos.y < Screen.height; } /// /// 移动到目标点 /// /// public void MoveToTarget(Transform trageTransform) { if (isClose) return; isClose = true; target = trageTransform; Vector3 offset = CarObj.transform.position - target.position; CarObj.GetComponent().ResetRotation(); CarmerMain.transform.DOMove(target.transform.position, 2); CarmerMain.transform.DORotateQuaternion(target.transform.rotation, 2).OnComplete(() => { CarObj.transform.parent = CarmerMain.transform; CarmerMain.transform.position = new Vector3(0, 0, -10); CarmerMain.transform.localEulerAngles = Vector3.zero; CarObj.transform.parent = CarParent; SetCurrentYRotationY(); //SetMinRDistance(); isClose = false; }); } /// /// 设置相机位置 /// public void SetCameraPos() { CarObj.transform.parent = CarmerMain.transform; CarmerMain.transform.position = new Vector3(0, 0, -10); CarmerMain.transform.localEulerAngles = Vector3.zero; CarObj.transform.parent = CarParent; SetCurrentYRotationY(); // SetMinRDistance(); } }