SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
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

4 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class Compass : MonoBehaviour
  7. {
  8. public GameObject iconPrefab;
  9. List<TracingMarker> tracingMarkers = new List<TracingMarker>();
  10. public RawImage compassImage;
  11. public Transform player;
  12. float compassUnit;
  13. public TracingMarker[] ss;
  14. public float maxDistance = 200f;
  15. private void OnEnable()
  16. {
  17. compassUnit = compassImage.rectTransform.rect.width / 360f;
  18. AddTracingMarker(ss[0]);
  19. AddTracingMarker(ss[1]);
  20. AddTracingMarker(ss[2]);
  21. AddTracingMarker(ss[3]);
  22. }
  23. private void Update()
  24. {
  25. compassImage.uvRect = new Rect(player.localEulerAngles.y / 360, 0f, 1f, 1f);
  26. foreach (TracingMarker marker in tracingMarkers)
  27. {
  28. marker.image.rectTransform.anchoredPosition = GetPoseOnCompass(marker);
  29. float dst = Vector2.Distance(new Vector2(player.transform.position.x, player.transform.position.z), marker.position);
  30. float scale = 0f;
  31. if(dst < maxDistance)
  32. {
  33. scale = 1f - (dst / maxDistance);
  34. marker.image.rectTransform.localScale = Vector3.one * scale;
  35. ; }
  36. }
  37. }
  38. public void AddTracingMarker (TracingMarker marker)
  39. {
  40. GameObject newMarker = Instantiate(iconPrefab, compassImage.transform);
  41. marker.image = newMarker.GetComponent<Image>();
  42. marker.image.sprite = marker.icon;
  43. tracingMarkers.Add(marker);
  44. }
  45. Vector2 GetPoseOnCompass (TracingMarker marker)
  46. {
  47. Vector2 cameraPos = new Vector2(player.transform.position.x, player.transform.position.z);
  48. Vector2 cameraPwd = new Vector2(player.transform.forward.x, player.transform.position.z);
  49. float angle = Vector2.SignedAngle(marker.position - cameraPos, cameraPwd);
  50. return new Vector2(compassUnit * angle, 0f);
  51. }
  52. }