2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
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.

95 lines
3.8 KiB

5 years ago
  1. using System.Collections.Generic;
  2. using Unity.FPS.Game;
  3. using Unity.FPS.Gameplay;
  4. using UnityEngine;
  5. namespace Unity.FPS.UI
  6. {
  7. public class Compass : MonoBehaviour
  8. {
  9. public RectTransform CompasRect;
  10. public float VisibilityAngle = 180f;
  11. public float HeightDifferenceMultiplier = 2f;
  12. public float MinScale = 0.5f;
  13. public float DistanceMinScale = 50f;
  14. public float CompasMarginRatio = 0.8f;
  15. public GameObject MarkerDirectionPrefab;
  16. Transform m_PlayerTransform;
  17. Dictionary<Transform, CompassMarker> m_ElementsDictionnary = new Dictionary<Transform, CompassMarker>();
  18. float m_WidthMultiplier;
  19. float m_HeightOffset;
  20. void Awake()
  21. {
  22. PlayerCharacterController playerCharacterController = FindObjectOfType<PlayerCharacterController>();
  23. DebugUtility.HandleErrorIfNullFindObject<PlayerCharacterController, Compass>(playerCharacterController,
  24. this);
  25. m_PlayerTransform = playerCharacterController.transform;
  26. m_WidthMultiplier = CompasRect.rect.width / VisibilityAngle;
  27. m_HeightOffset = -CompasRect.rect.height / 2;
  28. }
  29. void Update()
  30. {
  31. // this is all very WIP, and needs to be reworked
  32. foreach (var element in m_ElementsDictionnary)
  33. {
  34. float distanceRatio = 1;
  35. float heightDifference = 0;
  36. float angle;
  37. if (element.Value.IsDirection)
  38. {
  39. angle = Vector3.SignedAngle(m_PlayerTransform.forward,
  40. element.Key.transform.localPosition.normalized, Vector3.up);
  41. }
  42. else
  43. {
  44. Vector3 targetDir = (element.Key.transform.position - m_PlayerTransform.position).normalized;
  45. targetDir = Vector3.ProjectOnPlane(targetDir, Vector3.up);
  46. Vector3 playerForward = Vector3.ProjectOnPlane(m_PlayerTransform.forward, Vector3.up);
  47. angle = Vector3.SignedAngle(playerForward, targetDir, Vector3.up);
  48. Vector3 directionVector = element.Key.transform.position - m_PlayerTransform.position;
  49. heightDifference = (directionVector.y) * HeightDifferenceMultiplier;
  50. heightDifference = Mathf.Clamp(heightDifference, -CompasRect.rect.height / 2 * CompasMarginRatio,
  51. CompasRect.rect.height / 2 * CompasMarginRatio);
  52. distanceRatio = directionVector.magnitude / DistanceMinScale;
  53. distanceRatio = Mathf.Clamp01(distanceRatio);
  54. }
  55. if (angle > -VisibilityAngle / 2 && angle < VisibilityAngle / 2)
  56. {
  57. element.Value.CanvasGroup.alpha = 1;
  58. element.Value.CanvasGroup.transform.localPosition = new Vector2(m_WidthMultiplier * angle,
  59. heightDifference + m_HeightOffset);
  60. element.Value.CanvasGroup.transform.localScale =
  61. Vector3.one * Mathf.Lerp(1, MinScale, distanceRatio);
  62. }
  63. else
  64. {
  65. element.Value.CanvasGroup.alpha = 0;
  66. }
  67. }
  68. }
  69. public void RegisterCompassElement(Transform element, CompassMarker marker)
  70. {
  71. marker.transform.SetParent(CompasRect);
  72. m_ElementsDictionnary.Add(element, marker);
  73. }
  74. public void UnregisterCompassElement(Transform element)
  75. {
  76. if (m_ElementsDictionnary.TryGetValue(element, out CompassMarker marker) && marker.CanvasGroup != null)
  77. Destroy(marker.CanvasGroup.gameObject);
  78. m_ElementsDictionnary.Remove(element);
  79. }
  80. }
  81. }