It works fine when the plane lies on the z plane, ie when all the z coordinates are the same but when the plane is orientated any other way the value of distance is wrong.

Pffft. If I could read that code I might be able to help…

Are you trying to get the distance from an infinite plane or from a finite surface?

The infinite plane distance is simple:
construct a transformation matrix to map a point to the plane’s coordinate system, transform the point, inspect the Y coordinate - or whichever coordinate you have defined as “up”. That’s the distance.
Whether you see a negative coordinate as a positive distance or as 0 depends on what you want to do with it.
A somewhat more optimised method would be to subtract a base point (which is on the plane) from the point, then apply the inverse rotation matrix. That’s an optimisation because you don’t actually have to apply the whole matrix - only the Y unit vector (or whatever coordinate is “up”).

The finite plane would require you to determine if it’s on the outside or on the inside of the surface. On the inside, it would be the same as the infinite plane. On the outside, you’d have to find the nearest edge, then the nearest point on that edge, etc.,etc.

If you have your planes in point-normal form (or you can put them in that form), then what you can do is the following

Calculate a vector from the point on the plane to your free point.

Take the dot product this vector and the normal of the plane.

If your plane’s normal has unit length (it’s length is 1), then this dot product is your answer, otherwise it’s in terms of the length of the plane’s normal…