| 123456789101112131415161718192021222324252627282930313233343536373839 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- /* 靶子距离标签(在靶子头顶) */
- public class TargetDistanceLabel : MonoBehaviour
- {
- public RectTransform canvasRTF;
- public Transform followTarget;
- TargetBody targetBody;
- Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
- Text textComp;
- Canvas parentCanvas;
- void Start()
- {
- targetBody = GameObject.Find("GameArea/TargetObject").GetComponentInChildren<TargetBody>();
- textComp = this.GetComponent<Text>();
- parentCanvas = transform.parent.GetComponent<Canvas>();
- }
- void LateUpdate()
- {
- Camera mainCamera = BowCamera.ins.GetRenderCamera();
- parentCanvas.worldCamera = mainCamera;
- float angle = Vector3.Angle(mainCamera.transform.forward, followTarget.position - mainCamera.transform.position);
- if (angle > 90) {
- textComp.enabled = false;
- return;
- } else {
- textComp.enabled = true;
- }
- Vector3 v3 = mainCamera.WorldToViewportPoint(followTarget.position) - centerPoint;
- v3.x *= canvasRTF.rect.width;
- v3.y *= canvasRTF.rect.height;
- v3.z = 0;
- this.transform.localPosition = v3;
- textComp.text = ((int)Mathf.Round(targetBody.GetDistance())) + "M";
- }
- }
|