0
\$\begingroup\$

How do you convert the near clip plane and far clip planes to position units, to be able to find out its distance to a game object?

\$\endgroup\$
0
\$\begingroup\$

The near and far clip planes are already measured in world position units.

public struct ClipPlaneDistances {
    public float fromFarPlane;
    public float fromNearPlane;
}

public static ClipPlaneDistances GetDistancesFromClipPlanes(Camera camera, Vector3 point) {
    Vector3 fromCamera = point - camera.transform.position;
    float depth = Vector3.Dot(fromCamera, camera.transform.forward);

    ClipPlaneDistances distances;
    distances.fromNearPlane = depth - camera.nearClipPlane;
    distances.fromFarPlane = camera.farClipPlane - depth;

    return distances;
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.