Beispiel #1
0
        public void MoveToCloestPoint(Vector3 point)
        {
            int count = m_Path.Count;

            float bestDis    = float.PositiveInfinity;
            float bestFactor = 0;
            int   bestIndex  = 0;

            for (int i = 0; i < count - 1; i++)
            {
                Vector3 from       = m_Path[i];
                Vector3 to         = m_Path[i + 1];
                float   factor     = VectorMath.ClosestPointOnSegmentFactor(from, to, point);
                Vector3 pointOnSeg = Vector3.Lerp(from, to, factor);

                float distance = (pointOnSeg - point).sqrMagnitude;
                if (bestDis > distance)
                {
                    bestDis    = distance;
                    bestFactor = factor;
                    bestIndex  = i;
                }
            }

            MoveToSegment(bestIndex, bestFactor);
        }