_xiaofang/xiaofang/Assets/Script/UI/DistanceDisplay3D.cs

36 lines
1.1 KiB
C#
Raw Normal View History

2024-10-21 10:08:49 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2024-10-21 16:51:26 +08:00
//<2F><>ʾ<EFBFBD><CABE><EFBFBD>Ҿ<EFBFBD><D2BE><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
2024-10-21 10:08:49 +08:00
public class DistanceDisplay3D : MonoBehaviour
{
public Transform Character; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Transform
public Transform target; // Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Transform
public Text distanceText; // 3D Canvas <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Text <20><><EFBFBD><EFBFBD>
private Transform canvasTransform; // 3D UI Canvas <20><> Transform
void Start()
{
canvasTransform = distanceText.transform.parent; // <20><>ȡ Canvas <20><> Transform
}
void Update()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Һ<EFBFBD>Ŀ<EFBFBD><C4BF>֮<EFBFBD><D6AE><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
float distance = Vector3.Distance(Character.position, target.position)-4;
// <20><><EFBFBD><EFBFBD> UI <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λС<CEBB><D0A1>
distanceText.text = "<22><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>: " + distance.ToString("F0") + " <20><>";
// <20><> 3D UI Canvas <20>ƶ<EFBFBD><C6B6><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>
canvasTransform.position = target.position + new Vector3(0, 5f, 0); // <20><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD> 2 <20><>
canvasTransform.LookAt(Character); // <20><> 3D UI <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
canvasTransform.Rotate(0, 180, 0); // <20><>ת Canvas <20><> y <20>ᣬȷ<E1A3AC><C8B7><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD>ת
}
}