/** Calculates target point from the current line segment. * \param p Current position * \param a Line segment start * \param b Line segment end * The returned point will lie somewhere on the line segment. * \see #forwardLook * \todo This function uses .magnitude quite a lot, can it be optimized? */ protected Vector3 CalculateTargetPoint(Vector3 p, Vector3 a, Vector3 b) { a.y = p.y; b.y = p.y; float magn = (a - b).magnitude; if (magn == 0) { return(a); } float closest = Mathfx.Clamp01(Mathfx.NearestPointFactor(a, b, p)); Vector3 point = (b - a) * closest + a; float distance = (point - p).magnitude; float lookAhead = Mathf.Clamp(forwardLook - distance, 0.0F, forwardLook); float offset = lookAhead / magn; offset = Mathf.Clamp(offset + closest, 0.0F, 1.0F); return((b - a) * offset + a); }