This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/UI/WaypointMarker.cs

52 lines
1.6 KiB
C#
Raw Permalink Normal View History

2023-06-01 17:03:48 +02:00
using TMPro;
using UnityEngine;
using UnityEngine.UI;
2023-06-02 06:30:58 +02:00
namespace UI
{
2023-06-02 06:30:58 +02:00
public class WaypointMarker : MonoBehaviour
{
2023-06-02 06:30:58 +02:00
public Image pointer;
public Transform target;
public TMP_Text distanceMarker;
2023-06-02 06:30:58 +02:00
private Vector2 offset;
2023-06-02 06:30:58 +02:00
// Start is called before the first frame update
private void Start()
{
offset = distanceMarker.transform.position - pointer.transform.position;
}
2023-06-02 06:30:58 +02:00
// Update is called once per frame
private void Update()
{
2023-06-02 06:30:58 +02:00
var minX = pointer.GetPixelAdjustedRect().width / 2;
var maxX = Screen.width - minX;
var minY = pointer.GetPixelAdjustedRect().height / 2;
var maxY = Screen.height - minY;
Vector2 pos = Camera.main.WorldToScreenPoint(target.position);
if (Vector3.Dot(target.position - Camera.main.transform.position, Camera.main.transform.forward) < 0)
{
//target is behind player
if (pos.x < Screen.width / 2)
pos.x = maxX;
else
pos.x = minX;
}
pos.x = Mathf.Clamp(pos.x, minX, maxX);
pos.y = Mathf.Clamp(pos.y, minY, maxY);
pointer.transform.position = pos;
distanceMarker.text =
Mathf.RoundToInt(Vector3.Distance(Camera.main.transform.position, target.transform.position)) + " meters";
2023-06-01 17:03:48 +02:00
if (pos.x < Screen.width / 2)
2023-06-02 06:30:58 +02:00
distanceMarker.transform.position = pos + offset;
else
2023-06-02 06:30:58 +02:00
distanceMarker.transform.position = pos - offset;
}
}
2023-06-02 06:30:58 +02:00
}