using System.Collections; using System.Collections.Generic; using JetBrains.Annotations; using UnityEngine; using UnityEngine.EventSystems; public class JoystickController : MonoBehaviour, IDragHandler, IEndDragHandler { public bool isDrag = false; public RectTransform joystickBackground; // 摇杆背景 public RectTransform joystick; // 摇杆 public Camera playerCamera; // 摄像机 public float rotationSpeed = 0.5f; // 摄像机旋转速度 public Transform target; // 旋转角度限制 public float horizontalMinRotation = -75f; // 水平旋转最小值 public float horizontalMaxRotation = 75f; // 水平旋转最大值 public float verticalMinRotation = -75f; // 垂直旋转最小值 public float verticalMaxRotation = 75f; // 垂直旋转最大值 public Vector2 joystickInput = Vector2.zero; private float currentXRotation = 0f; // 当前的水平旋转角度 private float currentYRotation = 0f; // 当前的垂直旋转角度 private float targetXRotation = 0f; // 目标水平旋转角度 private float targetYRotation = 0f; // 目标垂直旋转角度 void Start() { // 初始化摇杆的位置 joystick.anchoredPosition = Vector2.zero; // 初始化摄像机的旋转角度 targetXRotation = playerCamera.transform.eulerAngles.y; targetYRotation = playerCamera.transform.eulerAngles.x; } // 每帧更新摇杆的输入 void Update() { if (joystickInput.magnitude > 0) { // 根据摇杆的输入控制摄像机旋转 targetXRotation = joystickInput.x * rotationSpeed * Time.deltaTime; targetYRotation = joystickInput.y * rotationSpeed * Time.deltaTime; float angleDifference = Mathf.Abs(playerCamera.transform.eulerAngles.x - target.transform.eulerAngles.x); float angleDifference2 = Mathf.Abs(playerCamera.transform.eulerAngles.y - target.transform.eulerAngles.y); Debug.Log(angleDifference); Debug.Log(angleDifference2); // if (playerCamera.transform.eulerAngles.x<-30) return; if (Mathf.Abs(playerCamera.transform.eulerAngles.x-target.transform.eulerAngles.x) <330&& Mathf.Abs(playerCamera.transform.eulerAngles.x - target.transform.eulerAngles.x) > 30) { return; } if (Mathf.Abs(playerCamera.transform.eulerAngles.y - target.transform.eulerAngles.y) < 315 && Mathf.Abs(playerCamera.transform.eulerAngles.y - target.transform.eulerAngles.y) > 45) { return; } //float yRotation = currentRotation.y; //if (yRotation > 45f) targetYRotation = 0f; //if (yRotation <45f) targetYRotation = 0f; //// 限制水平旋转范围,防止摄像机旋转过头 //targetXRotation = Mathf.Clamp(targetXRotation, horizontalMinRotation, horizontalMaxRotation); //// 限制垂直旋转范围,防止摄像机翻转 playerCamera.transform.RotateAround(target.transform.position, playerCamera.transform.right, targetYRotation); playerCamera.transform.RotateAround(target.transform.position, Vector3.up, targetXRotation); targetXRotation = playerCamera.transform.eulerAngles.y; targetYRotation = playerCamera.transform.eulerAngles.x; playerCamera.transform.SetParent(target); //playerCamera.transform.LookAt(new Vector3(target.transform.position.x,playerCamera.transform.position.y,target.transform.position.z)); // 使用 Quaternion 来平滑过渡到目标旋转角度 //playerCamera.transform.localRotation = Quaternion.Euler(targetYRotation, targetXRotation, 0f); } else { playerCamera.transform.SetParent(target.parent); targetXRotation = 0; targetYRotation = 0; } } // 摇杆拖动时更新摇杆位置和输入 public void OnDrag(PointerEventData eventData) { Vector2 localPoint; // 将触摸位置从屏幕坐标转换为UI本地坐标 RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBackground, eventData.position, eventData.pressEventCamera, out localPoint); // 计算摇杆的输入方向 joystickInput = localPoint.normalized; // 限制摇杆的移动范围,确保它不会超出背景的边界 if (localPoint.magnitude > joystickBackground.sizeDelta.x / 2) { joystickInput = localPoint.normalized * (joystickBackground.sizeDelta.x / 2); } // 更新摇杆的位置 joystick.anchoredPosition = joystickInput; } // 摇杆松开时重置摇杆位置 public void OnEndDrag(PointerEventData eventData) { isDrag = false; // 摇杆松开后重置位置 joystick.anchoredPosition = Vector2.zero; joystickInput = Vector2.zero; } }