converting pixels to unityunits

what is the conversion forumla for converting pixels to unity units, i checked other articles and for some reason their solution didn’t work for me. if this was already answered or another article i may not have seen has the solution, please put the lin but if you know it, can you please answer. by the way it not for gui stuff , it for working with scaling transforms with screen. Thanks for anyone who answer ahead of time.

For an Orthographic camera, the conversion can be done this way:

var worldToPixels = ((Screen.height / 2.0f) / Camera.main.orthographicSize);

‘worldToPixels’ can be multiplied by any world distance to give back pixel distance.

For a perspective camera, the conversion will depend on the distance away from the camera you take the measurement. And a conversion factor will only work if the object you are measuring is parallel to the plane of the camera. The conversion can be done this way:

var worldToPixels = Screen.height / (2.0 * distance * Mathf.Tan(Camera.main.fieldOfView * 0.5 * Mathf.Deg2Rad));

‘distance’ is the distance away from the camera.

Reference page: