- Home /

# [Still unsolved] Is there some way to cast lines and get the point where they intersect?

So I need the distance between two points.

However, this is harder when the objects are rotated, since the point I need to get is no longer the x and y coordinates of the others (1, 6).

Hope someone answers. Writing in paint was really hard...

**Answer** by IvovdMarel
·
5 days ago

I recommend using transform.InverseTransformPoint to calculate the local position of the collision. You should only have to look at the x or z value then (as the origin would be 0,0,0)

Sorry, but how? I tried this and the collisions weren´t were they were supposed to:

```
void OnCollisionStay2D (Collision2D coll)
{
for ( int l = 0; l < coll.contacts.Length; l++)
{
if (this.transform.position.x > coll.contacts[l].point.x)
{
leftPos = this.transform.InverseTransformPoint (coll.contacts[l].point);
}
}
for (int r = 0; r < coll.contacts.Length; r++)
{
if (this.transform.position.x < coll.contacts[r].point.x)
{
rightPos = this.transform.InverseTransformPoint(coll.contacts[r].point);
}
}
diff = Mathf.Abs(rightPos.x - leftPos.x);
}
```

```
void OnCollisionStay2D (Collision2D coll)
{
for ( int l = 0; l < coll.contacts.Length; l++)
{
Vector3 globalPos = coll.contacts[l].point;
Vector3 localPos = this.transform.InverseTransformPoint(globalPos);
if (localPos.x < leftPos.x)
{
leftPos = localPos;
} else if (localPos.x > rightPos.x)
{
rightPos = localPos;
}
}
diff = Mathf.Abs(rightPos.x - leftPos.x);
}
```

This should get the most left position and the most right position and calculate the difference. I didn't test it though so there might be some problems.

**Answer** by Bunny83
·
5 days ago

The distance between two "points" of course requires you to use all coordinates. Just looking at the x value (no matter if it's local or world space) of course doesn't work in every case.

From your code it's not clear what you actually need. There could be more than 2 collision points and it's not clear which ones you actually want. If you want to find the two points which are the farthest apart it becomes a bit more complicated. You would need to check each collision point against every other to find the greatest distance.

Basically something like that:

```
int count = coll.contacts.Length;
Vector3 point0;
Vector3 point1;
float distSqr = 0;
for ( int i = 0; i < count - 1; i++)
{
Vector3 p0 = coll.contacts[i].point;
for (int n = i+1; n < count; n++)
{
Vector3 p1 = coll.contacts[i].point;
float d = (p0-p1).sqrMagnitude;
if (d > distSqr)
{
point0 = p0;
point1 = p1;
distSqr = d;
}
}
}
float distance = Mathf.Sqrt(distSqr);
```

Here "point0" and "point1" will contain the two points which are farthest apart from each other and "distance" will contain the distance between those two points. You get the same by using:

`float distance = Vector3.Distance(point0, point1);`

at the end

Well, thanks, but I actually need to do this: https://gamedev.stackexchange.com/questions/113149/how-can-i-squeeze-a-game-object-when-it-is-pressed-between-two-others/113290 I made some changes to that code, but as you can see, that doesn´t work if the obects are rotated. Getting the distance between the furthest points doesn´t work either. If the squeezers are not perfectly aligned, the distance between those collisions will be bigger than the actual object´s width. That´s why I wanted only the x distance. When the objects aren´t rotated is just x - x and ignoring y and z axis.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How do I create an effective spawn point script for multiple scenes and change music each scene? 1 Answer

Points when an enemy dies. 2 Answers

Move object around another custom-made object 1 Answer

Find two points which are on 120 degrees from a third point. 3 Answers

domination capture points 1 Answer