Assets/Scripts/Utils.cs
/// <summary>
/// Intersects a plane with a line
/// </summary>
/// <param name="planeOrigin">The origin of the plane</param>
/// <param name="planeNormal">The normal of the plane</param>
/// <param name="lineOrigin">The origin of the line</param>
/// <param name="lineDirection">The direction of the line</param>
/// <param name="intersectionPoint">The point where the line hit the plane (if any)</param>
/// <returns>The collision result</returns>
Intersects a plane with a line
Intersects a plane with a line
public static PlaneLineIntersectionResult PlaneLineIntersection(float3 planeOrigin, float3 planeNormal, float3 lineOrigin,
            float3 lineDirection, out float3 intersectionPoint)
        {
            planeNormal = math.normalize(planeNormal);
            lineDirection = math.normalize(lineDirection);
            if (math.dot(planeNormal, lineDirection) == 0)
            {
                intersectionPoint = float3.zero;
                return (planeOrigin - lineOrigin).Equals(float3.zero) ? PlaneLineIntersectionResult.ParallelInsidePlane : PlaneLineIntersectionResult.NoHit;
            }
            var d = math.dot(planeOrigin, -planeNormal);
            var t = -(d + lineOrigin.z * planeNormal.z + lineOrigin.y * planeNormal.y + lineOrigin.x * planeNormal.x) / (lineDirection.z * planeNormal.z + lineDirection.y * planeNormal.y + lineDirection.x * planeNormal.x);
            intersectionPoint = lineOrigin + t * lineDirection;
            return PlaneLineIntersectionResult.OneHit;
        }
public static PlaneLineIntersectionResult PlaneLineIntersection ( float3 planeOrigin , float3 planeNormal , float3 lineOrigin , float3 lineDirection , out float3 intersectionPoint ) { planeNormal = math . normalize ( planeNormal ) ; lineDirection = math . normalize ( lineDirection ) ; if ( math . dot ( planeNormal , lineDirection ) == 0 ) { intersectionPoint = float3 . zero ; return ( planeOrigin - lineOrigin ) . Equals ( float3 . zero ) ? PlaneLineIntersectionResult . ParallelInsidePlane : PlaneLineIntersectionResult . NoHit ; } var d = math . dot ( planeOrigin , - planeNormal ) ; var t = - ( d + lineOrigin . z * planeNormal . z + lineOrigin . y * planeNormal . y + lineOrigin . x * planeNormal . x ) / ( lineDirection . z * planeNormal . z + lineDirection . y * planeNormal . y + lineDirection . x * planeNormal . x ) ; intersectionPoint = lineOrigin + t * lineDirection ; return PlaneLineIntersectionResult . OneHit ; }
Intersects a plane with a line
Intersects a plane with a line
The collision result
The collision result
planeOrigin
float3
The origin of the plane
The origin of the plane
planeNormal
float3
The normal of the plane
The normal of the plane
lineOrigin
float3
The origin of the line
The origin of the line
lineDirection
float3
The direction of the line
The direction of the line
float3
out
intersectionPoint
The point where the line hit the plane (if any)
The point where the line hit the plane ( if any )