I'm following the algorithm on on this website for my line segment intersection test code. But when looking at my line segment and plane intersect it doesn't produce the correct point of intersection.
Here is an example of what I'm talking about.
Here is my code for the test
public static bool SegmentPlane(Vector3 p1, Vector3 p2, float distance, Vector3 normal, out float time, out Vector3 point)
{
time = 1f;
point = Vector3.zero;
time = (Vector3.Dot(normal, normal * distance) - Vector3.Dot(normal, p1)) / Vector3.Dot(normal, (p2 - p1).normalized);
if(time >= 0f && time <= 1f)
{
point = p1 + time * (p2 - p1).normalized;
return true;
}
return false;
}
Display Code
public void OnDrawGizmos()
{
plane = new Plane(transform.forward, transform.position);
colliding = CollisionLibrary.SegmentPlane(p1.position,p2.position,Vector3.Distance(Vector3.zero,transform.position),transform.up,out time,out point);
if (!colliding)
Gizmos.color = Color.yellow;
else
Gizmos.color = Color.red;
Gizmos.DrawLine(p1.position, p2.position);
Gizmos.DrawSphere(point, .1f);
Gizmos.color = Color.white;
Gizmos.DrawLine(transform.position, transform.position+transform.up * 100);
}