public IEnumerator Navigate(Vector3 destination) { var remainingDistance = Mathf.Infinity; // if destination is too little we rotate towards target first if (Vector3.Distance(transform.position, destination) < MinimumDistancetoTurn) { yield return(StartCoroutine(AffineUtility.RotateTowards(transform, destination))); } // set the new destination and resume the navigation if (!SetDestination(destination)) { yield break; } // now wait till we reach the destination while (!DestinationReached) { // sometimes, due to the animation we may pass the target instead of reaching it // as a result agent then spins around the final target // therefore we turn agent towards target in case it passes it if (remainingDistance < DistanceToTarget) { // turn towards object Pause(); yield return(StartCoroutine(AffineUtility.RotateTowards(transform, destination))); Resume(); } remainingDistance = DistanceToTarget; yield return(null); } yield break; }
IEnumerator RotateTowardsTarget(Vector3 target, bool finish = false) { rotatingTowardsTarget = true; // Debug.Log("Rotating towrds target"); yield return(StartCoroutine(AffineUtility.RotateTowards(agent.transform, target, 1f))); // Debug.Log("Resume"); navAgent.Resume(); // after rotation we may finish if (finish) { Debug.Log("Shall finish"); if (Success()) { EndAction(true); } } rotatingTowardsTarget = false; }
Vector3 GetPosition(Vector3 offset) { var p = transform.position + offset; return(AffineUtility.RotateAroundPoint(p, transform.position, transform.rotation)); }