UI箭頭引導(dǎo)目標(biāo)點(diǎn)

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;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容