He said he “checked with a raycast that its 3m away” which means he used a raycast once and it seemed to have worked fine, but he didn’t say why that method was not what he wanted.
Subtracting the two will also just get the distance from the two object’s origin points, guessing from his images and explanation he doesn’t want.
I think your wall has made me more confused, but I think I understand what you want from your second attempt’s code. What does it give as the distance when you use that method to flatten the distance? By all means it looks like the math should work out.
I see. Now I really understand what you want, you want the distance between a point and a plane. You can’t get the distance you want with just 2 points as input, you need the rotation to be able to calculate this, or some other way to define which “way” the plane is facing. Plenty of math formulas online showing you how to do that.
Well, you do understand that a raycast hits the surface of the wall while using the “position” (whereever you get that from) may be behind the wall, so further away. So what exactly is the given information and what do you want to know. The Distance method does return the correct distance. Flattening on the x-z plane is also correct if you’re just interested in that horizontal distance.
A raycast would almost always return you something different unless the “point” you’re referring to is actually at the surface of the wall. Though even then, where did you raycast against the wall? A wall has size and it matters where you hit it. If you hit the wall at an angle, the distance would be greater.
ps: I just saw your latest post, though it’s still not clear how you actually raycast against your wall / point. If you’re looking for the shortest distance to the plane / surface of the wall, that’s a different question. You really need to clarify what you actually want to do. You mentioned that you used a raycast for reference a couple of times. However a raycast needs a direction that you cast the ray into. So you may want to show how you actually used the raycast with proper screenshots / drawing explaining your situation. Making us speculate about your setup / problem doesn’t really help