public virtual IEnumerator FollowRoutine(Follower follower) { do { Vector3 heading = Vector3.RotateTowards(follower.trs.position, waypoints[follower.currentWaypoint].position, rotaRates[follower.currentWaypoint] * Time.deltaTime, moveRates[follower.currentWaypoint] * Time.deltaTime); follower.trs.position = heading; follower.trs.rotation = Quaternion.SlerpUnclamped(follower.trs.rotation, waypoints[follower.currentWaypoint].rotation, rotaRates[follower.currentWaypoint] * Time.deltaTime); if (follower.trs.position == waypoints[follower.currentWaypoint].position && ((waitForRota[follower.currentWaypoint] && follower.trs.rotation == waypoints[follower.currentWaypoint].rotation) || !waitForRota[follower.currentWaypoint])) { if (invokeAfterReachedWaypoint[follower.currentWaypoint] != null) { invokeAfterReachedWaypoint[follower.currentWaypoint].onClick.Invoke(); } if (follower.currentWaypoint == waypoints.Length - 1) { RemoveFollower(follower.trs); yield break; } if (followClosestWaypoint) { follower.currentWaypoint = waypoints.IndexOf(TransformExtensions.GetClosestTransform(waypoints, follower.trs)); } else { follower.currentWaypoint++; } } yield return(new WaitForEndOfFrame()); } while (true); }
public virtual void AddFollower(Transform followerTrs) { if (invokeOnStart != null) { invokeOnStart.onClick.Invoke(); } Follower follower = new Follower(); follower.trs = followerTrs; follower.currentWaypoint = 0; if (followClosestWaypoint) { follower.currentWaypoint = waypoints.IndexOf(TransformExtensions.GetClosestTransform(waypoints, followerTrs)); } followers = followers.Add(follower); StartCoroutine(FollowRoutine(follower)); }
public virtual float CalculateTotalCost(Transform potentialFollowerTrs) { float totalCost = 0; Transform currentWaypoint = null; for (int i = 0; i < waypoints.Length; i++) { if (followClosestWaypoint) { currentWaypoint = TransformExtensions.GetClosestTransform(waypoints, currentWaypoint); totalCost += Vector2.Distance(currentWaypoint.position, potentialFollowerTrs.position) + wayPointCosts[waypoints.IndexOf(currentWaypoint)]; if (currentWaypoint == waypoints[waypoints.Length - 1]) { return(totalCost); } } else { totalCost += Vector2.Distance(waypoints[i].position, potentialFollowerTrs.position) + wayPointCosts[i]; } } return(totalCost); }