You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.9 KiB
61 lines
1.9 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class Compass : MonoBehaviour
|
|
{
|
|
public GameObject iconPrefab;
|
|
List<TracingMarker> tracingMarkers = new List<TracingMarker>();
|
|
|
|
public RawImage compassImage;
|
|
public Transform player;
|
|
|
|
float compassUnit;
|
|
public TracingMarker[] ss;
|
|
public float maxDistance = 200f;
|
|
private void OnEnable()
|
|
{
|
|
compassUnit = compassImage.rectTransform.rect.width / 360f;
|
|
AddTracingMarker(ss[0]);
|
|
AddTracingMarker(ss[1]);
|
|
AddTracingMarker(ss[2]);
|
|
AddTracingMarker(ss[3]);
|
|
}
|
|
private void Update()
|
|
{
|
|
compassImage.uvRect = new Rect(player.localEulerAngles.y / 360, 0f, 1f, 1f);
|
|
foreach (TracingMarker marker in tracingMarkers)
|
|
{
|
|
marker.image.rectTransform.anchoredPosition = GetPoseOnCompass(marker);
|
|
|
|
float dst = Vector2.Distance(new Vector2(player.transform.position.x, player.transform.position.z), marker.position);
|
|
float scale = 0f;
|
|
if(dst < maxDistance)
|
|
{
|
|
scale = 1f - (dst / maxDistance);
|
|
|
|
marker.image.rectTransform.localScale = Vector3.one * scale;
|
|
; }
|
|
}
|
|
}
|
|
|
|
public void AddTracingMarker (TracingMarker marker)
|
|
{
|
|
GameObject newMarker = Instantiate(iconPrefab, compassImage.transform);
|
|
marker.image = newMarker.GetComponent<Image>();
|
|
|
|
marker.image.sprite = marker.icon;
|
|
|
|
tracingMarkers.Add(marker);
|
|
}
|
|
Vector2 GetPoseOnCompass (TracingMarker marker)
|
|
{
|
|
Vector2 cameraPos = new Vector2(player.transform.position.x, player.transform.position.z);
|
|
Vector2 cameraPwd = new Vector2(player.transform.forward.x, player.transform.position.z);
|
|
|
|
float angle = Vector2.SignedAngle(marker.position - cameraPos, cameraPwd);
|
|
return new Vector2(compassUnit * angle, 0f);
|
|
}
|
|
}
|