Hi!
I wanted to use the new UI system for player units & enemy healthbars in a 3D RTS game, but i got stuck in several aspects:
1.) Scaling: I design on the big screen and want to resize it to fit on an a small unit. When i set the scale of the panel it’s pretty unpredictable & small. As well as positioning (see below) gets even more screwed up.
2.) Positioning: I want to position it dynamically above moving gameObjects in 3D Space.
// get screen position of gameobject:
Vector3 unitPos = Camera.main.WorldToScreenPoint(transform.position);
// try to find the center by getting bounds and later dividing bounds.x by 2:
Rect r = rootPanel.worldBound; // world bounds might be wrong, but local also doesn't work
float width = r.size.x;
Vector3 v = new(unitPos.x - width/2, Screen.height - unitPos.y);
rootPanel.transform.position = v;
It almost works, but it’s not centered correctly and seems to be shifting left and right when the gameobject is moving around.
As a simple implementation, i will probably create a UI panel for each gameobject. More performant would be if I move around several VisualElements for all gameobjects on one single Panel, i guess.
Thanks, Maria!