It’s fixed now.
My first failure was, that I only checked, if the intersection Point was on the line, but not on the segment.
Than I changed the algorithm for checking the intersection point in Triangle to:
private bool LineIntersectionInTriangle(Vector3 planePoint0, Vector3 planePoint1, Vector3 planePoint2, Vector3 intersection)
{
Vector3 u = planePoint2 - planePoint0;
Vector3 v = planePoint1 - planePoint0;
Vector3 w = intersection - planePoint0;
float s = ((Vector3.Dot(u,v) * Vector3.Dot(w,v)) - (Vector3.Dot(v,v) * Vector3.Dot(w,u))) / ((Mathf.Pow(Vector3.Dot(u,v),2)) - (Vector3.Dot(u,u)*Vector3.Dot(v,v)));
float t = ((Vector3.Dot(u, v) * Vector3.Dot(w, u)) - (Vector3.Dot(u, u) * Vector3.Dot(w, v))) / ((Mathf.Pow(Vector3.Dot(u, v), 2)) - (Vector3.Dot(u, u) * Vector3.Dot(v, v)));
if ((s >= 0 && t >= 0 && s + t <= 1) || ((s == 0 && t == 0) || s + t == 1))
return true;
else
return false;
}
standard algorithm
So now I check first the intersection with line segment to plane:
//Get the intersection between a line and a plane.
//If the line and plane are not parallel, the function outputs true, otherwise false.
public static bool LinePlaneIntersection(out Vector3 intersection, Vector3 linePoint, Vector3 lineVec, float magnitude, Vector3 planeNormal, Vector3 planePoint)
{
float length;
float dotNumerator;
float dotDenominator;
Vector3 vector;
intersection = Vector3.zero;
//calculate the distance between the linePoint and the line-plane intersection point
dotNumerator = Vector3.Dot((planePoint - linePoint), planeNormal);
dotDenominator = Vector3.Dot(lineVec, planeNormal);
//line and plane are not parallel
if (dotDenominator != 0.0f)
{
length = dotNumerator / dotDenominator;
if (0 <= length && length <= magnitude)
{
//create a vector from the linePoint to the intersection point
vector = SetVectorLength(lineVec, length);
//get the coordinates of the line-plane intersection point
intersection = linePoint + vector;
return true;
}
else // intersection but not in the length of magnitude
return false;
}
//output not valid
else
return false;
}
algorithm from http://wiki.unity3d.com/index.php/3d_Math_functions adapted by me
and than i check if the intersection point is in triangle(code is on top)