I’m struggling with health bars in Unity and hoping someone can point me in the right direction. Here’s the situation: I want to create health bars that stay right above my units and always face the camera, no matter how I move the camera around. I’ve tried a bunch of different things, but nothing is quite doing the trick.
First, I attempted using a **world-space canvas**. I thought this would be a good approach, but I can’t seem to get the health bar to stay properly positioned above the unit. When I tried setting the rotation with this code:
“`csharp
transform.rotation = Quaternion.LookRotation(transform.position – Camera.main.transform.position);
“`
it didn’t yield the desired results. I also experimented with variations, such as swapping the minus for a plus, but that didn’t work either. My brain is a bit fried trying to wrap my head around how all of this should be working together.
Then, I switched to a **screen-space canvas**, which got me closer, but I’m still having issues. The health bar slightly shifts when I move the camera, and it also changes size when I zoom in and out, which is super frustrating. I used this code for positioning:
“`csharp
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 fiddled with things like freezing the Y and Z rotations, but it doesn’t seem to help much either. Ideally, what I want is for these health bars to stay perfectly horizontal and aligned above my units, just like how they are in League of Legends. I keep visualizing the sleekness of that setup, and it’s driving me nuts that I can’t replicate it.
If anyone has insights or suggestions on how to achieve this in Unity, I would love to hear them! It would make a world of difference for my project. Thanks!
Health Bars in Unity: Keeping Them Aligned and Facing the Camera
It sounds like you’re really getting into the nitty-gritty of health bars in Unity! This can definitely be tricky, especially when you want them to always face the camera and stay positioned above your units. Here are a few tips that might help you get closer to that League of Legends feel:
Using a World-Space Canvas
If you still want to go with a world-space canvas, make sure that the health bar’s pivot point is set correctly. The rotation code you’ve shared is almost there, but you might want to tweak it a bit:
This way, it should be rotating to face the camera correctly. Also, ensure that the health bar is positioned based on the unit’s position, like so:
Screen-Space Canvas Adjustments
With a screen-space canvas, it sounds like the health bar’s position shifts because the canvas can be affected by camera movements in a way that world-space elements are not. You can try keeping the health bar always aligned horizontally by adjusting its rotation every frame:
Keeping It Stable
If the health bar’s size changes with zooming, consider setting its scale each frame based on the camera’s distance:
Final Touches
Remember to have an offset for the Y position so that it’s above the unit properly. Basically, give it a height that feels right with:
Keep tweaking these values until it feels just right! Game devs always have to play with numbers to get what looks good. Good luck, and I hope this helps you get those health bars looking like you want!
A common and effective way to achieve smoothly aligned health bars that stay above your units, similar to League of Legends, is to use a world-space canvas positioned as a child of each unit, combined with an orientation adjustment so the UI always faces the camera. Instead of manually adjusting rotations, a more reliable method is to set the canvas rotation each frame using Unity’s built-in function
transform.LookAt()
. Specifically, within your health bar script, utilize this code snippet in the LateUpdate method:transform.LookAt(transform.position + Camera.main.transform.forward);
. This ensures the health bar directly faces the camera without any unusual flipping or tilting issues that sometimes occur from manual quaternion calculations.If you’d prefer retaining a screen-space UI canvas for consistency, avoid using direct screen position assignments, as these may cause jitter or resizing when zooming or rotating the camera. Instead, consider using Unity’s built-in class
RectTransformUtility.WorldToScreenPoint()
combined with proper anchors of your UI elements. Setting the UI anchor to center-top and updating its position each frame using the converted world-to-screen point generally provides smoother results. Additionally, ensure your camera reference is cached and accurate to avoid computational overhead and jittering caused by frequent lookups. These adjustments will help your health bars remain smoothly aligned, properly sized, and visually consistent, providing the professional polish you’re aiming for.