Intersectionpoint in Triangle?

Hi,
pleace excuse my english.
i try to figure out, if Intersectionpoint (Vector3 Q) is in a Triangle ( Vector3 A, B, C).
for example:
Q = “(0.5, 1.0, 0.5)”
A = “(0.5, -0.5, 0.5)”
B = “(-0.5, 0.5, 0.5)”
C = “(-0.5, -0.5, 0.5)”
In this case is Q not in the Triangle, but my code says it is Triangle.
Does any one see the issue and can explain me what I am doing wrong?

private void addToListIfIntersectionIsInTriangle(Vector3 Q, Vector3 A, Vector3 B, Vector3 C, Vector3 n)
    {
        n.Normalize(); // normal of Triangle

        float u = Vector3.Dot(Vector3.Cross((B - A), (Q - A)), n);
        float v = Vector3.Dot(Vector3.Cross((C - B), (Q - B)), n);
        float w = Vector3.Dot(Vector3.Cross((A - C), (Q - C)), n);

        if( u >= 0 && v >=0 && w>=0)
        {
            if (!newVertices.Contains(Q))
            {
                Debug.Log("1");
            }
        }
   }

This post sounds similar: Point In Triangle Code - C# - Unity Engine - Unity Discussions

From what I remember I thought at least one of your cross product values needed to be normalised but I’m probably wrong.

thank you for help, but normalise the cross products doesn’t change anything on the result.

i tried many things out, it seems like this function only works for non planar triangles, as soon a triangle planar (like (1,2,0),(1,2,3),(3,2,3)), this function tests if the Intersection Point is hover over the Triangle… hopefully you understand what i mean.

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)