How to make HPBar follow 3d world object?

I googled a lot and most of them I found does not work at all.

Here is my current code.

public class MyUIFollow : MonoBehaviour {

    public Camera cam;
    public Transform Target;
    private RectTransform rt;
    public Vector3 offset = Vector3.up;   
    public Canvas canvas;
    void Start ()
    {
        cam = GameObject.Find("CanvasCamera").GetComponent<Camera>();
        rt = GetComponent<RectTransform>();
    }  
    
    void Update () {
        rt.position = BattleStatus.GetScreenPosition(Target, canvas, cam);
    }
}

and

public static Vector3 GetScreenPosition(Transform transform, Canvas canvas, Camera cam)
    {
        Vector3 pos;
        float width = canvas.GetComponent<RectTransform>().sizeDelta.x;
        float height = canvas.GetComponent<RectTransform>().sizeDelta.y;
        float x = cam.WorldToScreenPoint(transform.position).x / Screen.width;
        float y = cam.WorldToScreenPoint(transform.position).y / Screen.height;
        pos = new Vector3(width * x - width / 2, y * height - height / 2);
        return pos;
    }

This does not work, and worldtoscreenpoint does not work also.

Anyone has working solution?

Thanks.

My wanting is simple. Just make Hpbar (unity new gui) follow scene’s 3d character model’s above.

Someone proposed using text mesh, but isn’t it less crisp than unity ui?

I put this on the panel:

RectTransform rt;
    [SerializeField] Transform target;
   // Use this for initialization
   void Start () {
        rt = GetComponent<RectTransform>();       
   }
   
   // Update is called once per frame
   void Update () {
        rt.position = Camera.main.WorldToScreenPoint(target.position + new Vector3(.2f,.2f,0));
   }

It’s ‘tiny bit’ up and to the right of the player’s head (that’s the target i put in the inspector). I set the pivot to the bottom left, but I’m sure you could adjust that to how you like it. It’s working perfectly for me. You could also get it to change position only on move, which could be more efficient, maybe.