Home / Forums / Drawing Centered Player Names & Health Bars in Unity C# OnGUI

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Source

Drawing Centered Player Names & Health Bars in Unity C# OnGUI

ParanormalDev
MelonLoader / BepInEx Dev
MEMBER
Rep: 230
Join Date: Sep 2023
Posts: 21
Thanks: 58
2y ago · Sep 28, 2023 4:45 PM
#1
Here is a complete helper for drawing centered player names above the player head and a dynamic colored health bar alongside the bounding box in Unity:

CSHARP
using UnityEngine;

public static class NameHealthESP
{
    private static GUIStyle _nameStyle;
    public static GUIStyle NameStyle
    {
        get
        {
            if (_nameStyle == null)
            {
                _nameStyle = new GUIStyle(GUI.skin.label)
                {
                    alignment = TextAnchor.MiddleCenter,
                    fontSize = 12,
                    fontStyle = FontStyle.Bold
                };
                _nameStyle.normal.textColor = Color.white;
            }
            return _nameStyle;
        }
    }

    public static void DrawPlayerDetails(float boxX, float boxY, float boxWidth, float boxHeight, string name, float currentHealth, float maxHealth)
    {
        // 1. Draw Player Name above Box
        GUIContent content = new GUIContent(name);
        Vector2 size = NameStyle.CalcSize(content);
        float nameX = boxX + (boxWidth * 0.5f) - (size.x * 0.5f);
        float nameY = boxY - size.y - 3f;

        // Black drop shadow
        GUI.color = Color.black;
        GUI.Label(new Rect(nameX + 1, nameY + 1, size.x, size.y), content, NameStyle);
        GUI.color = Color.white;
        GUI.Label(new Rect(nameX, nameY, size.x, size.y), content, NameStyle);

        // 2. Draw Health Bar on Left Side
        float barWidth = 4f;
        float barX = boxX - barWidth - 3f;
        float healthPercent = Mathf.Clamp01(currentHealth / maxHealth);
        float healthHeight = boxHeight * healthPercent;
        float healthY = boxY + (boxHeight - healthHeight);

        // Background (Black)
        GUI.color = new Color(0f, 0f, 0f, 0.7f);
        GUI.DrawTexture(new Rect(barX, boxY, barWidth, boxHeight), Texture2D.whiteTexture);

        // Foreground Health Color (Green -> Yellow -> Red)
        Color healthColor = Color.Lerp(Color.red, Color.green, healthPercent);
        GUI.color = healthColor;
        GUI.DrawTexture(new Rect(barX, healthY, barWidth, healthHeight), Texture2D.whiteTexture);
    }
}
ParanormalDev | MelonLoader & ImGui Modding
The following users thanked ParanormalDev for this post:
GlowShader
Shader & Overlay Artist
MEMBER
Rep: 185
Join Date: Oct 2023
Posts: 36
Thanks: 46
2y ago · Sep 28, 2023 6:20 PM
#2
CSHARP
Color.Lerp(Color.red, Color.green, healthPercent)
provides that smooth gradient shift as damage is taken. Clean and fast!
GlowShader | Shaders, Stencils & DirectX DrawLists
VectorByte
Graphics & DirectX Dev
VIP
Rep: 420
Join Date: Aug 2022
Posts: 39
Thanks: 115
2y ago · Sep 29, 2023 11:10 AM
#3
Adding a 1px border around the health bar makes it look high-end against bright backgrounds.
VectorByte | DirectX 11/12 Hooking & ImGui Overlays
Quote
Math is the language of game engines.