bool MoveToNextPoint(float TimeDelta, Pathfinding.TerrainPath targetPath, ref int currentPathIndex) { if (targetPath == null || targetPath.waypoints.Count < currentPathIndex + 2) { currentPathIndex = 0; return(true); } Vector3 targetPosition = targetPath.waypoints[currentPathIndex + 1].worldPosition; Vector3 result = (targetPosition - transform.position).normalized * TimeDelta * moveSpeed; forwardVector = result.normalized; if ((targetPosition - transform.position + result).magnitude < objectiveDistanceOffset) { transform.position = new Vector3(targetPosition.x, transform.position.y, targetPosition.z); currentPathIndex++; if (targetPath.waypoints.Count < currentPathIndex) { currentPathIndex = 0; return(true); } return(false); } else { transform.Translate(result); return(false); } }
private IEnumerator GoToTargetAndInvokeTask(Vector3 targetPosition) { Pathfinding.TerrainPath pathToTarget = pathfinder.FindPath(transform.position, targetPosition); int pathIndex = 0; while (!MoveToNextPoint(Time.deltaTime, pathToTarget, ref pathIndex)) { if (interruptMovement) { interruptMovement = false; yield break; } yield return(null); } if (OnTargetReached != null) { OnTargetReached.Invoke(); } }