public void FollowPath(Path6_6 path)
    {
        // Predict the future location of the body.
        Vector2 predictedLocation = body.position + body.velocity.normalized * 2.5f;

        float   distanceRecord = float.MaxValue;
        Vector2 recordTarget   = Vector2.zero;

        // Look at each segment and find the closest normal point.
        for (int i = 0; i < path.points.Length - 1; i++)
        {
            Vector2 a           = path.points[i].position;
            Vector2 b           = path.points[i + 1].position;
            Vector2 normalPoint = GetNormalPoint(predictedLocation, a, b);
            // If the normal point is beyond the line segment, clamp it to the endpoint.
            if (normalPoint.x > b.x || normalPoint.x < a.x)
            {
                normalPoint = b;
            }

            // If this point is closer than any previous point, update the record.
            float distance = Vector2.Distance(predictedLocation, normalPoint);
            if (distance < distanceRecord)
            {
                distanceRecord = distance;
                recordTarget   = normalPoint;
            }
        }

        // Is the vehicle predicted to leave the path?
        if (distanceRecord > path.radius)
        {
            // If so, steer the vehicle towards the path.
            Seek(recordTarget);
        }

        #region Debug Line Drawing
        // Send the information that was calculated to the debug lines drawer.
        DrawDebugLines(
            body.position, predictedLocation,
            predictedLocation, recordTarget
            );
        #endregion
    }
Esempio n. 2
0
    public void pathFollow(Path6_6 path)
    {
        float distance = new float();

        Vector3 predict = velocity;
        Vector3 pNormal = predict.normalized;

        pNormal *= 20;
        Vector3 predictedLocation = location + pNormal;

        /*
         * for (int i = 0; i < path.pathVectors.Count - 1; i++)
         * {
         *  Vector3 a = path.pathVectors[i];
         *  Vector3 b = path.pathVectors[i + 1];
         *
         *  Vector3 normalPoint = getNormalPoint(predictedLocation, a, b);
         *  if(normalPoint.x < a.x || normalPoint.x > b.x)
         *  {
         *      normalPoint = b;
         *  }
         *
         *  distance = Vector3.Distance(normalPoint, predictedLocation);
         *
         *  if (distance > 10)
         *  {
         *      Vector3 target = normalPoint;
         *      seek(target);
         *  }
         *  else
         *  {
         *      wander();
         *  }
         * }
         */
    }