public void FollowPath() { /* * predictedLocation = is the predicted location the gameobject will reach with * its current velocity. * pointA = is the vector from the start of the path, to the predicted location. * pointB = is the vector from the start of the path, to the end of the path. */ Vector2 predictedLoc = GetPredictedLocation(predictionDistance); Vector2 pointA = Vector2.zero; Vector2 pointB = Vector2.zero; Vector2 normalPoint = Vector2.zero; float bestDistance = startingBestDistance; Vector2 targetNormalPoint = Vector2.zero; for (int i = 0; i < pathSize; i++) { pointA = path[i].start; pointA = path[i].start; pointB = path[i].end; /* * normalPoint = the point where the shark should be in the path, from its predicted location. */ normalPoint = GetNormalPoint(predictedLoc, pointA, pointB); if (!InPath(normalPoint, pointA, pointB)) { normalPoint = pointB; } float normalDistance = VectorUtility.distance(predictedLoc, normalPoint); if (normalDistance < bestDistance) { bestDistance = normalDistance; targetNormalPoint = normalPoint; } } /* * normalPoint = the point where the shark should be in the path, from its predicted location. */ Vector2 direction = pointB - pointA; direction = direction.normalized; direction *= directionDistance; Vector2 target = targetNormalPoint + direction; float distance = VectorUtility.distance(targetNormalPoint, predictedLoc); pathNormalLR.SetPosition(0, predictedLoc); pathNormalLR.SetPosition(1, targetNormalPoint); desiredVelocityLR.SetPosition(0, predictedLoc); desiredVelocityLR.SetPosition(1, transform.position); if (distance > radius) { pathNormalLR.SetColors(Color.red, Color.red); sb.Move(target); } else { pathNormalLR.SetColors(Color.green, Color.green); } }