Here is what I want to achieve, health bars that stay perfectly above the units, regardless of where the unit moves or where the camera is: https://www.youtube.com/live/fZSaPDz-GJc?si=B868wwF5KAPCuEtw&t=3305
I've tried all sort of things, both having my canvas in world-space and screen-space.
In world-space I cannot get my canvas to properly stay in position above the unit, while always facing the camera.
transform.rotation = Quaternion.LookRotation(transform.position - Camera.main.transform.position); I also tried without - Camera.main.transform.position and also with + instead, nothing has the desired result and I can't quite wrap my brain around how all of this works. Another thing I tried is this, both with and without the y freeze.
Vector3 forward = _cam.transform.forward; forward.y = 0; forward.Normalize(); healthbarCanvas.transform.forward = forward; This makes the edges of my health bar go up/down as the camera moves, it doesn't stay horizontal like it should.
In screen space I got pretty close, but it's still moving a bit when I'm moving my camera and the health bar also gets bigger/smaller as I zoom in/out.
public override void OnLateUpdateView() { Vector3 offset = new Vector3(0, 3.5f, 0); Vector3 worldPos = transform.position + offset; Vector3 screenPos = _cam.WorldToScreenPoint(worldPos); healthbar.transform.position = screenPos; } I've tried several variations, like freezing the y rotation, freezing the z rotation, but nothing works exactly as I need it to work.
I want health bars that stay exactly above my unit as it moves around in 3D space, regardless of camera position, angle or zoom. Exactly like the health bars in League of Legends above the champions and minions work.
As suggested I tried:
transform.rotation = Quaternion.LookRotation(camera.forward, camera.up); Which results in this: From far:

Someone in the Unity Discord suggested using an Ortographic camera instead, which does solve this issue perfectly. Is it possible with a Perspective camera?
