Get Flat Distance with Different Rotations

Hey, i am currently struggeling a bit getting the Distance from the purple capsule, to the blue wall

So I know you can get the distance like this:

  var distance = Vector3.Distance(transform.position, wall.transform.position);

and i also tried to flatten the distance like

 Vector3 my2dPos = transform.position;
        my2dPos.y = 0;
        Vector3 target2dPos = wall.transform.position;      
        target2dPos.y = 0;
        var distanceToTarget = Vector3.Distance(my2dPos, target2dPos);

but this is also not working out
.

.

the blue box got a rotation of 0, 45, 0
and the purple capsule is rotated -45, -45, 90
.

.

i placed the capsule 3m away from the box and also checked with a raycast that its 3m away,

but i would like to know, how to calculate the distance only by knowing the XYZ position of both objects

200292-upload-2022-9-30-14-6-13.png

I was hoping someone might come in with a simple suggestion but not so far…

It strikes me the best way is to use Plane.GetDistanceToPoint. To do so, you’d have to create a plane on the front face of the cube. Were I doing that, I’d make the cube a child of an empty game object, position the cube so that it’s entirely behind the point, with the point at centre bottom of the front face. Then use use something like:

Vector3 cubePos = cubeParent.transform.position; Plane plane = new Plane(cubePos, Vector3.Distance(Vector3.zero, cubePos)); print(plane.GetDistanceToPoint(capsule.transform.position));

The plane doesn’t actually appear in the hierarchy so doesn’t interfere with the game at all. The downside is that it measures to the centre of the Capsule, which may or may not be what you want. As above, you can put a parent on the capsule if you want to measure to a different point.