DistanceViewTarget.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // MIT License
  2. // https://gitlab.com/ilnprj
  3. // Copyright (c) 2020 ilnprj
  4. using UnityEngine.UI;
  5. using UnityEngine;
  6. namespace RadarComponents
  7. {
  8. /// <summary>
  9. /// Distance view metric in uni-meters
  10. /// </summary>
  11. [RequireComponent(typeof(Text))]
  12. public class DistanceViewTarget : AbstractExtensionTarget
  13. {
  14. private Text textDistance;
  15. [Header("Image target for alpha check:")]
  16. [SerializeField]
  17. private Image imageTarget = default;
  18. [Header("Changing alpha text:")]
  19. [SerializeField]
  20. private bool changeAlphaText = default;
  21. private void Awake()
  22. {
  23. textDistance = GetComponent<Text>();
  24. }
  25. public override void UpdateExtensionView(Transform playerPosition,ITarget inputTarget)
  26. {
  27. Vector3 dif = playerPosition.position - inputTarget.TransformTarget.position;
  28. float lenght = dif.magnitude;
  29. textDistance.text = lenght.ToString("0")+"m";
  30. if (changeAlphaText && imageTarget)
  31. {
  32. textDistance.color = new Color(textDistance.color.r, textDistance.color.g, textDistance.color.b, imageTarget.color.a);
  33. }
  34. }
  35. }
  36. }