Here’s something that will calculate where two lines will intersect. If they don’t intersect then the two resulting vectors will mark the points where both lines are closest together.
/* setup vectors */ Vector3 l1begin = playerCurrentPosition; Vector3 l1end = playerTargetPosition - l1begin; Vector3 l2begin = enemyCurrentPosition; Vector3 l2end = enemyTargetPosition - l2begin;
/* calculate vectors */ Vector3 n = Vector3.Cross(l1end, l2end); Vector3 u = Vector3.Cross(n, l1begin - l2begin) / Vector3.Dot(n, n); Vector3 intersection1 = l1begin - l1end * Vector3.Dot(l2end, u); Vector3 intersection2 = l2begin - l2end * Vector3.Dot(l1end, u);
/* draw vectors */ Debug.DrawRay(l1begin, l1end, Color.cyan); Debug.DrawRay(l2begin, l2end, Color.magenta); Debug.DrawLine(intersection1, intersection2, Color.white);