TargetMiniMap3D.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // MIT License
  2. // https://gitlab.com/ilnprj
  3. // Copyright (c) 2020 ilnprj
  4. // Modified: Jingyi
  5. using UnityEngine;
  6. namespace RadarComponents
  7. {
  8. [RequireComponent(typeof(MeshRenderer))]
  9. public class TargetMiniMap3D : BaseTargetView
  10. {
  11. public int mapSize = 2;
  12. private float radarWidth;
  13. private float radarHeight;
  14. /// <summary>
  15. /// Given that the target has the same sides
  16. /// </summary>
  17. private float targetWidth;
  18. private MiniMap3D radarContainer;
  19. private MeshRenderer modelTargetView;
  20. protected override void Awake()
  21. {
  22. base.Awake();
  23. radarContainer = GetComponentInParent<MiniMap3D>();
  24. modelTargetView = GetComponent<MeshRenderer>();
  25. }
  26. private void Start()
  27. {
  28. radarWidth = radarContainer.transform.localScale.x;
  29. radarHeight = radarContainer.transform.localScale.y;
  30. transform.SetParent(radarContainer.transform);
  31. targetWidth = radarWidth * 5 / 100;
  32. UpdateViewTarget();
  33. }
  34. public override void UpdateViewTarget()
  35. {
  36. Vector3 playerPos = playerTransform.position;
  37. Vector3 targetPos = CurrentTarget.TransformTarget.position;
  38. Vector3 normalisedTargetPosiiton = NormalisedPosition(playerPos, targetPos);
  39. Vector2 targetPosition = CalculateBlipPosition(normalisedTargetPosiiton);
  40. targetPosition.x = CheckBorder(targetPosition.x, 5 );
  41. targetPosition.y = CheckBorder(targetPosition.y, 5 );
  42. if (radarContainer.TargetsFadeOut)
  43. {
  44. Vector3 dif = targetPos - playerPos;
  45. float distance = dif.magnitude;
  46. modelTargetView.enabled = distance <= radarContainer.InsideRadarDistance;
  47. }
  48. UpdateResultPosition(targetPosition);
  49. }
  50. /// <summary>
  51. /// Controls the location of the target within
  52. /// </summary>
  53. private float CheckBorder(float position, float border)
  54. {
  55. if (position + targetWidth >= border)
  56. {
  57. position = border - targetWidth;
  58. }
  59. if (position < -5)
  60. {
  61. position = -5;
  62. }
  63. return position;
  64. }
  65. private void UpdateResultPosition(Vector2 position)
  66. {
  67. transform.localPosition = new Vector3(position.x, 2, -position.y);
  68. }
  69. private Vector3 NormalisedPosition(Vector3 playerPos, Vector3 targetPos)
  70. {
  71. float normalisedyTargetX =(targetPos.x - playerPos.x) / radarWidth;
  72. float normalisedyTargetZ =(targetPos.z - playerPos.z) / radarWidth;
  73. return new Vector3(normalisedyTargetX, 0, normalisedyTargetZ);
  74. }
  75. private Vector2 CalculateBlipPosition(Vector3 targetPos)
  76. {
  77. float angleToTarget = Mathf.Atan2(targetPos.x, targetPos.z) * Mathf.Rad2Deg;
  78. float anglePlayer = playerTransform.eulerAngles.y;
  79. // Angle
  80. float angleRadarDegrees = angleToTarget - anglePlayer - 90;
  81. // Calculate
  82. float normalisedDistanceToTarget = targetPos.magnitude / mapSize ;
  83. float angleRadians = angleRadarDegrees * Mathf.Deg2Rad;
  84. float tarX = normalisedDistanceToTarget * Mathf.Cos(angleRadians);
  85. float tarY = normalisedDistanceToTarget * Mathf.Sin(angleRadians);
  86. // Scale
  87. tarX *= radarWidth / 2;
  88. tarY *= radarHeight / 2;
  89. // Offset
  90. if (radarContainer.OffsetPosition != 0)
  91. {
  92. tarX += radarWidth / radarContainer.OffsetPosition;
  93. tarY += radarHeight / radarContainer.OffsetPosition;
  94. }
  95. return new Vector2(tarX, tarY);
  96. }
  97. }
  98. }