Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 39678
In Process

askthedev.com Latest Questions

Asked: June 15, 20252025-06-15T22:14:13+05:30 2025-06-15T22:14:13+05:30

How can I ensure health bars in Unity stay above units and consistently face the camera regardless of camera movement?

anonymous user

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!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2025-06-15T22:14:15+05:30Added an answer on June 15, 2025 at 10:14 pm

      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:

      
      transform.rotation = Quaternion.LookRotation(Camera.main.transform.position - transform.position);
          

      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:

      
      transform.position = unit.transform.position + new Vector3(0, heightOffset, 0);
          

      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:

      
      healthbar.transform.LookAt(Camera.main.transform);
      healthbar.transform.rotation = Quaternion.Euler(0, healthbar.transform.rotation.eulerAngles.y, 0);
          

      Keeping It Stable

      If the health bar’s size changes with zooming, consider setting its scale each frame based on the camera’s distance:

      
      float distance = Vector3.Distance(Camera.main.transform.position, unit.transform.position);
      healthbar.transform.localScale = Vector3.one * (1 / 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:

      
      Vector3 worldPos = unit.transform.position + new Vector3(0, heightAboveUnit, 0);
          

      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!

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2025-06-15T22:14:16+05:30Added an answer on June 15, 2025 at 10:14 pm

      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.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Recent Answers

    1. anonymous user on Can we determine if it’s possible to escape from a given array structure?
    2. anonymous user on Can we determine if it’s possible to escape from a given array structure?
    3. anonymous user on How can I ensure health bars in Unity stay above units and consistently face the camera regardless of camera movement?
    4. anonymous user on How can I ensure health bars in Unity stay above units and consistently face the camera regardless of camera movement?
    5. anonymous user on Why are my wheat assets not visible from a distance despite increasing the detail distance in terrain settings?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.