Home / Forums / Calculating and Formatting 3D Distance Meters for Unity Entity ESP

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Calculating and Formatting 3D Distance Meters for Unity Entity ESP

AimbotMath
Trigonometry & Smoothing
MEMBER
Rep: 285
Join Date: Nov 2023
Posts: 27
Thanks: 73
2y ago · Nov 14, 2023 11:20 AM
#1
Displaying distance in meters below enemy bounding boxes helps players gauge threat priority and weapon damage falloff ranges.

Distance Calculation & Text Label Formatting:
CSHARP
using UnityEngine;

public static class DistanceESP
{
    public static void DrawDistance(Vector3 localPlayerPos, Vector3 targetPos, float boxX, float boxY, float boxWidth, float boxHeight)
    {
        // Calculate Euclidean distance in 3D world space
        float distance = Vector3.Distance(localPlayerPos, targetPos);

        string text = $"[{Mathf.RoundToInt(distance)}m]";
        GUIContent content = new GUIContent(text);
        
        GUIStyle distStyle = new GUIStyle(GUI.skin.label)
        {
            alignment = TextAnchor.MiddleCenter,
            fontSize = 10,
            fontStyle = FontStyle.Normal
        };
        distStyle.normal.textColor = new Color(0.85f, 0.85f, 0.85f, 0.9f);

        Vector2 size = distStyle.CalcSize(content);
        float posX = boxX + (boxWidth * 0.5f) - (size.x * 0.5f);
        float posY = boxY + boxHeight + 2f;

        // Shadow + Label
        GUI.color = Color.black;
        GUI.Label(new Rect(posX + 1, posY + 1, size.x, size.y), content, distStyle);
        GUI.color = Color.white;
        GUI.Label(new Rect(posX, posY, size.x, size.y), content, distStyle);
    }
}
AimbotMath · Angular Normalization & Smooth Angles
The following users thanked AimbotMath for this post:
KeycardMaster
Item ESP & Unity UI Dev
MEMBER
Rep: 215
Join Date: Oct 2023
Posts: 19
Thanks: 54
2y ago · Nov 14, 2023 1:45 PM
#2
Using
CODE
Mathf.RoundToInt()
instead of floating point decimals stops text jittering every frame when both players are walking. Great tip!
KeycardMaster | Keycard & Objective ESP Specialist
ZeroMemory
Member
MEMBER
Rep: 75
Join Date: Oct 2024
Posts: 32
Thanks: 18
2y ago · Nov 15, 2023 8:30 AM
#3
Combined this with NameESP and the ESP layout looks so clean now!
ZeroMemory · Always experimenting