using UnityEngine;
using UnityEngine.UI;
public class ArrowPointer : MonoBehaviour
{
? ? public Transform target; // 目標(biāo)物品的Transform
? ? public RectTransform arrowRect; // UI箭頭的RectTransform
? ? public RectTransform panelRect; // 包含箭頭的Panel的RectTransform
? ? public Camera camera; // 主相機(jī)
? ? public Vector2 arrowSize; // UI箭頭的大小
? ? public float minDistanceToHide = 1f; // 距離小于此值時(shí)隱藏箭頭
? ? void Update()
? ? {
? ? ? ? if (target != null && camera != null)
? ? ? ? {
? ? ? ? ? ? // 計(jì)算目標(biāo)物品與相機(jī)之間的距離
? ? ? ? ? ? float distanceToTarget = Vector3.Distance(target.position, camera.transform.position);
? ? ? ? ? ? // 計(jì)算目標(biāo)物品與相機(jī)之間的方向
? ? ? ? ? ? Vector3 directionToTarget = (target.position - camera.transform.position).normalized;
? ? ? ? ? ? // 檢查是否朝向目標(biāo)物體
? ? ? ? ? ? bool isFacingTarget = Vector3.Dot(camera.transform.forward, directionToTarget) > 0;
? ? ? ? ? ? // 如果距離小于1拉背,且朝向目標(biāo)物體师崎,則隱藏UI箭頭
? ? ? ? ? ? if (distanceToTarget < minDistanceToHide && isFacingTarget)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? arrowRect.gameObject.SetActive(false);
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? // 顯示UI箭頭
? ? ? ? ? ? arrowRect.gameObject.SetActive(true);
? ? ? ? ? ? // 將目標(biāo)物品的世界位置轉(zhuǎn)換到屏幕位置
? ? ? ? ? ? Vector3 screenPoint = camera.WorldToScreenPoint(target.position);
? ? ? ? ? ? // 將屏幕位置轉(zhuǎn)換到Panel的局部位置
? ? ? ? ? ? Vector2 localPoint;
? ? ? ? ? ? RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRect, screenPoint, null, out localPoint);
? ? ? ? ? ? // 計(jì)算箭頭方向并旋轉(zhuǎn)
? ? ? ? ? ? Vector3 direction = screenPoint - new Vector3(Screen.width / 2, Screen.height / 2, 0);
? ? ? ? ? ? float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
? ? ? ? ? ? arrowRect.localEulerAngles = new Vector3(0, 0, angle - 90); // 減去90度是因?yàn)槟J(rèn)箭頭向上
? ? ? ? ? ? // 調(diào)整箭頭位置,確保箭頭在Panel范圍內(nèi)且完整顯示
? ? ? ? ? ? AdjustArrowPosition(localPoint);
? ? ? ? }
? ? }
? ? void AdjustArrowPosition(Vector2 localPoint)
? ? {
? ? ? ? // 確保箭頭在Panel的邊界內(nèi)
? ? ? ? float halfArrowWidth = arrowSize.x / 2f;
? ? ? ? float halfArrowHeight = arrowSize.y / 2f;
? ? ? ? float panelWidth = panelRect.rect.width;
? ? ? ? float panelHeight = panelRect.rect.height;
? ? ? ? // 計(jì)算箭頭在Panel中的位置
? ? ? ? Vector2 clampedPosition = new Vector2(
? ? ? ? ? ? Mathf.Clamp(localPoint.x, -panelWidth / 2 + halfArrowWidth, panelWidth / 2 - halfArrowWidth),
? ? ? ? ? ? Mathf.Clamp(localPoint.y, -panelHeight / 2 + halfArrowHeight, panelHeight / 2 - halfArrowHeight)
? ? ? ? );
? ? ? ? // 如果目標(biāo)在左側(cè)椅棺,將箭頭放置在Panel的左邊界
? ? ? ? if (localPoint.x < -panelWidth / 2 + halfArrowWidth)
? ? ? ? {
? ? ? ? ? ? clampedPosition.x = -panelWidth / 2 + halfArrowWidth;
? ? ? ? }
? ? ? ? // 如果目標(biāo)在右側(cè)犁罩,將箭頭放置在Panel的右邊界
? ? ? ? if (localPoint.x > panelWidth / 2 - halfArrowWidth)
? ? ? ? {
? ? ? ? ? ? clampedPosition.x = panelWidth / 2 - halfArrowWidth;
? ? ? ? }
? ? ? ? // 如果目標(biāo)在上方或下方,將箭頭放置在Panel的上下邊界
? ? ? ? if (localPoint.y < -panelHeight / 2 + halfArrowHeight)
? ? ? ? {
? ? ? ? ? ? clampedPosition.y = -panelHeight / 2 + halfArrowHeight;
? ? ? ? }
? ? ? ? if (localPoint.y > panelHeight / 2 - halfArrowHeight)
? ? ? ? {
? ? ? ? ? ? clampedPosition.y = panelHeight / 2 - halfArrowHeight;
? ? ? ? }
? ? ? ? // 設(shè)置箭頭的位置
? ? ? ? arrowRect.localPosition = clampedPosition;
? ? }
}