93 lines
3.4 KiB
C#
93 lines
3.4 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
public class TargetIndicator : MonoBehaviour
|
|||
|
{
|
|||
|
public Camera mainCamera; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public Transform target; // Ŀ<><C4BF><EFBFBD><EFBFBD>
|
|||
|
public RectTransform uiCanvas; // UI Canvas <20><> RectTransform
|
|||
|
public RectTransform targetIcon; // Ŀ<><C4BF>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD> UI
|
|||
|
public Text distanceText; // <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
|
|||
|
public RectTransform arrowIcon; // <20><>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>ͷͼ<CDB7><CDBC>
|
|||
|
|
|||
|
public float edgePadding = 50f; // <20><>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB>Ե<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
|
|||
|
void Update()
|
|||
|
{
|
|||
|
// <20><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
|
|||
|
Vector3 screenPoint = mainCamera.WorldToScreenPoint(target.position);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB>
|
|||
|
if (screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < Screen.width && screenPoint.y > 0 && screenPoint.y < Screen.height)
|
|||
|
{
|
|||
|
// Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ʾĿ<CABE><C4BF>ͼ<EFBFBD><CDBC>
|
|||
|
targetIcon.gameObject.SetActive(true);
|
|||
|
arrowIcon.gameObject.SetActive(false); // <20><><EFBFBD>ؼ<EFBFBD>ͷ
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>λ<EFBFBD><CEBB>
|
|||
|
targetIcon.position = screenPoint;
|
|||
|
|
|||
|
// <20><><EFBFBD>¾<EFBFBD><C2BE><EFBFBD><EFBFBD>ı<EFBFBD>
|
|||
|
float distance = Vector3.Distance(mainCamera.transform.position, target.position);
|
|||
|
distanceText.text = $"{distance:F1}m";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>⣬<EFBFBD><E2A3AC>ʾ<EFBFBD><CABE>ͷ
|
|||
|
targetIcon.gameObject.SetActive(false);
|
|||
|
arrowIcon.gameObject.SetActive(true);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Һ<D2BA><F3B7BDA3><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
|
|||
|
if (screenPoint.z < 0)
|
|||
|
{
|
|||
|
screenPoint.x = Screen.width - screenPoint.x; // ˮƽ<CBAE><C6BD>ת
|
|||
|
screenPoint.y = Screen.height - screenPoint.y; // <20><>ֱ<EFBFBD><D6B1>ת
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>ĵķ<C4B5><C4B7><EFBFBD>
|
|||
|
Vector3 direction = (screenPoint - new Vector3(Screen.width / 2, Screen.height / 2, 0)).normalized;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷָ<CDB7><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>߽<EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
Vector3 edgePosition = GetScreenEdgePosition(direction);
|
|||
|
|
|||
|
// <20><><EFBFBD>ü<EFBFBD>ͷλ<CDB7><CEBB>
|
|||
|
arrowIcon.position = edgePosition;
|
|||
|
|
|||
|
// <20><>ת<EFBFBD><D7AA>ͷʹ<CDB7><CAB9>ָ<EFBFBD><D6B8>Ŀ<EFBFBD>귽<EFBFBD><EAB7BD>
|
|||
|
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
|||
|
arrowIcon.rotation = Quaternion.Euler(0, 0, angle);
|
|||
|
|
|||
|
// <20><><EFBFBD>¾<EFBFBD><C2BE><EFBFBD><EFBFBD>ı<EFBFBD>
|
|||
|
float distance = Vector3.Distance(mainCamera.transform.position, target.position);
|
|||
|
distanceText.text = $"{distance:F1}m";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>Ļ<EFBFBD>߽<EFBFBD><DFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
private Vector3 GetScreenEdgePosition(Vector3 direction)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
|
|||
|
float halfScreenWidth = Screen.width / 2 - edgePadding;
|
|||
|
float halfScreenHeight = Screen.height / 2 - edgePadding;
|
|||
|
|
|||
|
// <20>жϼ<D0B6>ͷ<EFBFBD>Ƿ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>Ե
|
|||
|
float slope = direction.y / direction.x;
|
|||
|
if (Mathf.Abs(slope) > halfScreenHeight / halfScreenWidth)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>Ե
|
|||
|
float edgeY = Mathf.Sign(direction.y) * halfScreenHeight + Screen.height / 2;
|
|||
|
float edgeX = (edgeY - Screen.height / 2) / slope + Screen.width / 2;
|
|||
|
return new Vector3(edgeX, edgeY, 0);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ұ<EFBFBD>Ե
|
|||
|
float edgeX = Mathf.Sign(direction.x) * halfScreenWidth + Screen.width / 2;
|
|||
|
float edgeY = slope * (edgeX - Screen.width / 2) + Screen.height / 2;
|
|||
|
return new Vector3(edgeX, edgeY, 0);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|