public override Vector2 get(Vector2 target, Vector2 currentVelocity, Vector2 targetVelocity = new Vector2()) { Vector2 direction = target - arrive.pos; float length = direction.magnitude; float speed = currentVelocity.magnitude; float predict; if (speed <= length / maxPredict) { predict = maxPredict; } else { predict = length / speed; } return(arrive.get(target + targetVelocity * predict, currentVelocity)); }
public override Vector2 get(Vector2 target, Vector2 currentVelocity, Vector2 targetVelocity = new Vector2()) { float minDist = (path[0] - (Vector2)owned.position).magnitude; int index = 0; int secondIndex; Vector2 forward; for (int i = 0; i < path.Length; ++i) { float dist = (path[i] - (Vector2)owned.position).magnitude; if (dist < minDist) { minDist = dist; index = i; } } if (index == 0) { secondIndex = 1; forward = path[secondIndex] - path[index]; } else if (index == path.Length - 1) { secondIndex = path.Length - 2; forward = path[index] - path[secondIndex]; } else { Vector2 toPrev = (Vector2)owned.position - path[index - 1]; Vector2 toNext = (Vector2)owned.position - path[index + 1]; forward = path[index + 1] - path[index - 1]; float prevMag = (path[index] - path[index - 1]).magnitude; float nextMag = (path[index] - path[index + 1]).magnitude; float projToPrev = Vector2.Dot(toPrev, path[index] - path[index - 1]) / prevMag; float projToNext = Vector2.Dot(toNext, path[index] - path[index + 1]) / nextMag; if (projToNext >= nextMag && projToPrev >= prevMag) { secondIndex = index; } else if (projToNext >= nextMag) { secondIndex = index - 1; } else if (projToPrev >= prevMag) { secondIndex = index + 1; } else if (projToNext > projToPrev) { secondIndex = index - 1; } else { secondIndex = index + 1; } } Vector2 objectDirection = (Vector2)owned.position - path[index]; Vector2 pathDirection = path[secondIndex] - path[index]; float len = Vector2.Dot(objectDirection, pathDirection) / pathDirection.magnitude; target = path[index]; if (len > Mathf.Epsilon) { target += pathDirection.normalized * len; float dot = Vector2.Dot(path[index] - target, forward); if (dot < 0) { ++index; } } else { ++index; } secondIndex = index + 1; len = (getOrLast(index) - target).magnitude; if (len > followDistance) { return(arrive.get(distanceLerp(target, getOrLast(index), followDistance), currentVelocity)); } len = followDistance - len; return(arrive.get(distanceLerp(getOrLast(index), getOrLast(secondIndex), len), currentVelocity)); }