I was debugging for a few hours and I think I have found the place the bug is in !

It’s inside TraingleMeshNode starting somewher close to line 316 !

I think this code is flawed, at first i have tried to find a bug in it, but later decided to write my own solution, and it worked much better,

ACTUAL SOURCE

```
Int3 a = GetVertex(first);
Int3 b = GetVertex((first+1)%av);
```

```
//The coordinate which is not the same for the vertices
int ocoord = coord == 2 ? 0 : 2;
```

```
//When the nodes are in different tiles, they might not share exactly the same edge
//so we clamp the portal to the segment of the edges which they both have.
int mincoord = System.Math.Min(a[ocoord], b[ocoord]);
int maxcoord = System.Math.Max(a[ocoord], b[ocoord]);
```

```
mincoord = System.Math.Max(mincoord, System.Math.Min(other.GetVertex(second)[ocoord], other.GetVertex((second+1)%bv)[ocoord]));
maxcoord = System.Math.Min(maxcoord, System.Math.Max(other.GetVertex(second)[ocoord], other.GetVertex((second+1)%bv)[ocoord]));
```

```
if (a[ocoord] < b[ocoord]) {
a[ocoord] = mincoord;
b[ocoord] = maxcoord;
} else {
a[ocoord] = maxcoord;
b[ocoord] = mincoord;
}
```

```
if (left != null) {
//All triangles should be clockwise so second is the rightmost vertex (seen from this node)
left.Add((Vector3)a);
right.Add((Vector3)b);
}
return true;
```

Here is my code, sometimes it still returns a path with some glitches (fast and dirty coding it was) but still it returns better result than actual solution.

MY SOLUTION

```
Int3 a = GetVertex(first);
Int3 b = GetVertex((first + 1) % av);
Int3 A = other.GetVertex(second);
Int3 B = other.GetVertex((second + 1) % bv);
Int3[] pts = new Int3[] { a, b, A, B };
Vector3 pt1 = Vector3.zero;
Vector3 pt2 = Vector3.zero;
Vector3[] combinations = (new Int3[] { a, b, A, a, b, B, A, B, a, A, B, b }).Convert<Int3, Vector3>(x => (Vector3)x);
int ptCount = 0;
for (int i = 0; i < combinations.Length / 3; i++) {
float baryc;
Vector3 segmPointVect = MathUtils.SegmentPointClosestVector(combinations[i * 3 + 0], combinations[i * 3 + 1], combinations[i * 3 + 2], out baryc);
if (baryc >= 0 || baryc <= 1) {
if (ptCount == 0) {
pt1 = (Vector3)combinations[i * 3 + 2] - segmPointVect * 0.5f;
} else {
pt2 = (Vector3)combinations[i * 3 + 2] - segmPointVect * 0.5f;
}
ptCount++;
}
if (ptCount >= 2) break;
}
Vector3 nodePos = (Vector3)this.position;
float sideSign = (pt2.x - pt1.x) * (nodePos.z - pt1.z) - (pt2.z - pt1.z) * (nodePos.x - pt1.x);
if(sideSign > 0) {
Vector3 tmp = pt1;
pt1 = pt2;
pt2 = tmp;
}
if (left != null) {
//All triangles should be clockwise so second is the rightmost vertex (seen from this node)
left.Add(pt1);
right.Add(pt2);
}
return true;
```