using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
//要跟随的血条,锚点设到中心
public RectTransform rectTransform;
//跟随的目标位置
public Transform up;
//血条父物体
public RectTransform canvasTransform;
//UI相机
public Camera UICamera;
private void Update()
{
//获取指定点在屏幕坐标的位置
Vector2 head = Camera.main.WorldToScreenPoint(up.position);
Vector2 locPos;
//将一个屏幕空间点转换为 canvasTransform 的本地空间中位于其矩形平面上的一个位置。
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasTransform, head, UICamera, out locPos);
rectTransform.anchoredPosition = locPos;
}
}