using UnityEngine; public class PointArrowInBox : MonoBehaviour { [SerializeField] Rect viewBox; [SerializeField] RectTransform arrow; [SerializeField] RectTransform target; Vector2 halfSize; private void Start() { halfSize = new Vector2(viewBox.width / 2, viewBox.height / 2); } private void LateUpdate() { Vector2 arrowPos = target.anchoredPosition; arrowPos.x = Mathf.Clamp(arrowPos.x, viewBox.x - halfSize.x, viewBox.x + halfSize.x); arrowPos.y = Mathf.Clamp(arrowPos.y, viewBox.y - halfSize.y, viewBox.y + halfSize.y); float arrowAngle = Mathf.Atan2(target.anchoredPosition.y - viewBox.position.y, target.anchoredPosition.x - viewBox.position.x) * Mathf.Rad2Deg; arrow.anchoredPosition = arrowPos; arrow.localEulerAngles = new Vector3(0, 0, arrowAngle); } public void SetViewBox(Rect viewBox) { this.viewBox = viewBox; halfSize = new Vector2(viewBox.width / 2, viewBox.height / 2); } }