TargetDistanceLabel.cs 813 B

1234567891011121314151617181920212223242526
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class TargetDistanceLabel : MonoBehaviour
  6. {
  7. public RectTransform canvasRTF;
  8. public Camera mainCamera;
  9. public Transform followTarget;
  10. TargetBody targetBody;
  11. Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
  12. void Start()
  13. {
  14. targetBody = GameObject.Find("GameArea/010").GetComponentInChildren<TargetBody>();
  15. }
  16. void LateUpdate()
  17. {
  18. Vector3 v3 = mainCamera.WorldToViewportPoint(followTarget.position) - centerPoint;
  19. v3.x *= canvasRTF.rect.width;
  20. v3.y *= canvasRTF.rect.height;
  21. v3.z = 0;
  22. this.transform.localPosition = v3;
  23. this.GetComponent<Text>().text = ((int)Mathf.Round(targetBody.GetDistance())) + "M";
  24. }
  25. }