3y ago · Jul 10, 2023 2:30 PM
In Unity games, projecting 3D entity positions onto the 2D screen coordinate space is simplified using .
How to calculate a dynamic 2D bounding box:
CSHARP
Camera.main.WorldToScreenPoint()How to calculate a dynamic 2D bounding box:
CSHARP
using UnityEngine;
public static class ESPUtils
{
private static Texture2D _whiteTexture;
public static Texture2D WhiteTexture
{
get
{
if (_whiteTexture == null)
{
_whiteTexture = new Texture2D(1, 1);
_whiteTexture.SetPixel(0, 0, Color.white);
_whiteTexture.Apply();
}
return _whiteTexture;
}
}
public static void DrawBox(float x, float y, float width, float height, float thickness, Color color)
{
GUI.color = color;
// Top & Bottom
GUI.DrawTexture(new Rect(x, y, width, thickness), WhiteTexture);
GUI.DrawTexture(new Rect(x, y + height - thickness, width, thickness), WhiteTexture);
// Left & Right
GUI.DrawTexture(new Rect(x, y, thickness, height), WhiteTexture);
GUI.DrawTexture(new Rect(x + width - thickness, y, thickness, height), WhiteTexture);
}
public static void Draw2DBoxESP(Vector3 feetPos, Vector3 headPos, Color boxColor)
{
Camera cam = Camera.main;
if (cam == null) return;
Vector3 screenFeet = cam.WorldToScreenPoint(feetPos);
Vector3 screenHead = cam.WorldToScreenPoint(headPos);
// Check if entity is behind the camera
if (screenFeet.z <= 0.01f || screenHead.z <= 0.01f) return;
// Invert Y coordinate for Unity OnGUI (screen space top-left is 0,0)
float feetY = Screen.height - screenFeet.y;
float headY = Screen.height - screenHead.y;
float height = feetY - headY;
float width = height * 0.55f; // Standard human aspect ratio
float x = screenFeet.x - (width * 0.5f);
DrawBox(x, headY, width, height, 1.5f, boxColor);
}
}
RustMechanic | IL2CPP & Unity Engine Analysis
The following users thanked RustMechanic for this post: