Distance between 2 objects without their Z axis.

I need to find the distance of two objects but ignoring the Z axis in the calculation. Normaly I would just do:

Vector3 aV = a.transform.position;
Vector3 bV = b.transform.position;
aV.z = 0f;
bV.z = 0f;
float dist = Vector3.Distance(aV,bV);

But that only works when I want Z to be the WorldSpace Z. My Z should be the local Z of the 2 objects (I make sure in advance that they face the same direction).

Is there maybe already something in Unity that I can use to easily figure that distance out?

Move b into a’s local space:

Vector3 bInASpace = a.transform.InverseTransformPoint(b.transform.position);

bInASpace.z = 0

Now bInASpace should be your flattened-to-A’s-local-XY-plane direction + distance.