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?