95 lines
2.4 KiB
C#
95 lines
2.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class EdgeIndicator : MonoBehaviour
|
|
{
|
|
[HideInInspector]
|
|
public EdgeTarget Target;
|
|
|
|
private RectTransform rect;
|
|
private Image image;
|
|
|
|
private RectTransform minimapRect;
|
|
private MinimapManager minimap;
|
|
|
|
private float radius;
|
|
|
|
public void Initialize(
|
|
EdgeTarget target,
|
|
RectTransform minimapRectTransform,
|
|
float edgePadding)
|
|
{
|
|
Target = target;
|
|
|
|
minimapRect = minimapRectTransform;
|
|
minimap = FindObjectOfType<MinimapManager>();
|
|
|
|
rect = GetComponent<RectTransform>();
|
|
image = GetComponent<Image>();
|
|
|
|
image.sprite = target.icon;
|
|
image.color = target.iconColor;
|
|
|
|
radius = Mathf.Min(minimapRect.rect.width, minimapRect.rect.height) * 0.5f - edgePadding;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Target == null)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
if (minimap == null || minimap.MinimapCamera == null || minimap.Player == null)
|
|
return;
|
|
|
|
Camera cam = minimap.MinimapCamera;
|
|
|
|
// Position of the target in the minimap camera
|
|
Vector3 viewport = cam.WorldToViewportPoint(Target.transform.position);
|
|
|
|
// If target is visible inside minimap, hide the edge arrow
|
|
bool visible =
|
|
viewport.z > 0 &&
|
|
viewport.x >= 0f &&
|
|
viewport.x <= 1f &&
|
|
viewport.y >= 0f &&
|
|
viewport.y <= 1f;
|
|
|
|
if (visible)
|
|
{
|
|
image.enabled = false;
|
|
return;
|
|
}
|
|
|
|
image.enabled = true;
|
|
|
|
// Direction from player to target
|
|
Vector3 worldDir = Target.transform.position - minimap.Player.position;
|
|
|
|
Vector2 dir = new Vector2(worldDir.x, worldDir.z).normalized;
|
|
|
|
// If your minimap rotates, rotate direction too
|
|
if (minimap.rotateWithPlayer)
|
|
{
|
|
float angle = -minimap.Player.eulerAngles.y * Mathf.Deg2Rad;
|
|
|
|
float cos = Mathf.Cos(angle);
|
|
float sin = Mathf.Sin(angle);
|
|
|
|
dir = new Vector2(
|
|
dir.x * cos - dir.y * sin,
|
|
dir.x * sin + dir.y * cos
|
|
);
|
|
}
|
|
|
|
// Place arrow on edge of minimap
|
|
rect.anchoredPosition = dir * radius;
|
|
|
|
// Rotate arrow to point toward target
|
|
float rot = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
|
|
|
|
rect.localRotation = Quaternion.Euler(0, 0, rot - 90f);
|
|
}
|
|
} |