# 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

What do you need to do that getting the distance from Raycast doesnâ€™t solve here?

I donâ€™t think he is raycasting anymore.

now this may be wrong, so maybe somebody can correct me if it is.

but can a distance be defined by getting the magnitude of one vector subtract the other ?

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.

There wont be an object at all there. Just a point. The blue wall is pretty much just for representation:)

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.

so the distance with the raycast is 3.00011 , with

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

Debug.Log("distance = " + distanceToTarget);
``````

i get distance = 3.68244

i also just placed some gizmos to see where the points are:

`````` private void OnDrawGizmos()
{
Vector3 my2dPos = transform.position;
my2dPos.y = 0;
Vector3 target2dPos = wall.transform.position;
target2dPos.y = 0;

Gizmos.color = Color.red;
Gizmos.DrawSphere(my2dPos, 0.1f);

Gizmos.color = Color.yellow;
Gizmos.DrawSphere(target2dPos, 0.1f);
}
``````

but it looks like its not alligned correct. like a bit to the left:

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.

1 Like

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

in case anybody will stumble over this in the future. this is what i wanted:

how you get it:

`````` var d1 = wall.position - transform.position;
var p = Vector3.Project(d1, -transform.up);

Debug.Log("project distance = " + p.magnitude.ToString("F2"));
``````
1 Like

Did I guess that?

you and RadRedPanda gave me the right terms or keywords to search for. so i found this:

Looks interesting I hadnâ€™t seen this before

1 Like