//===================================================== private IEnumerator Gathering(Action onComplete) { var wayPointIndex = Random.Range(0, _wayPoints.Count); while (true) { // Select new target for gem-gathering var lastIndex = wayPointIndex; do { wayPointIndex = Random.Range(0, _wayPoints.Count); }while(wayPointIndex == lastIndex); // Target gem if available var isTargetingGem = IsGemAvailable(); // Else target next waypoint for hunting / gathering gems if (isTargetingGem == false) { _thisAgent.SetDestination(_wayPoints[wayPointIndex].transform.position); } // Set walk speed _thisAgent.speed = _walkSpeed; _animation.Walk(); // Move toward target while ((_thisTransform.position - _thisAgent.destination).sqrMagnitude > 0.5f) { // Check for any new gems found if (isTargetingGem == false) { isTargetingGem = IsGemAvailable(); } //guard_motor.facingDirection = patrolPoints[i].position - _transform.position; yield return(null); } // Arrived at target ( gem or gathering point ) if (isTargetingGem == true) { // *** Gem gathered -> EAT gem *** if (onComplete != null) { onComplete(); } } //guard_motor.facingDirection = patrolPoints[i].position - _transform.position; // Stop movement _thisAgent.speed = 0.0f; _animation.Stop(); // Play audio fx _audioSource.PlayOneShot(_clipChatter); yield return(new WaitForSeconds(1.0f)); } }