public static Vector3 SegmentIntersectionPoint (Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2, out bool intersects) {
Vector3 dir1 = end1-start1;
Vector3 dir2 = end2-start2;
//Color rnd = new Color (Random.value,Random.value,Random.value);
//Debug.DrawRay (start1,dir1,rnd);
//Debug.DrawRay (start2,dir2,rnd);
float den = dir2.z*dir1.x - dir2.x * dir1.z;
if (den == 0) {
intersects = false;
return start1;
}
float nom = dir2.x*(start1.z-start2.z)- dir2.z*(start1.x-start2.x);
float nom2 = dir1.x*(start1.z-start2.z) - dir1.z * (start1.x - start2.x);
float u = nom/den;
float u2 = nom2/den;
if (u < 0F || u > 1F || u2 < 0F || u2 > 1F) {
intersects = false;
return start1;
}
//Debug.Log ("U1 "+u.ToString ("0.00")+" U2 "+u2.ToString ("0.00")+"\nP1: "+(start1 + dir1*u)+"\nP2: "+(start2 + dir2*u2)+"\nStart1: "+start1+" End1: "+end1);
//Debug.DrawLine (start2,end2,Color.magenta);
//Debug.DrawRay (start1,dir1*5,Color.green);
intersects = true;
return start1 + dir1*u;
}