private void ProcessFollowerChain(HashSet <GameObject> lemonings, Vector3 target) { HashSet <GameObject> leaders = new HashSet <GameObject>(); // Nearest lemon will follow cursor if (lemonings.Count > 0) { GameObject lemonObj = FindNearestLemoning(lemonings, target); LemoningController lemon = lemonObj.GetComponent <LemoningController>(); lemon.SetDestination(target); lemonings.Remove(lemonObj); leaders.Add(lemonObj); } while (lemonings.Count > 0) { // get next nearest lemon GameObject lemonObj = FindNearestLemoning(lemonings, target); // Find potential leader (one closest to this lemon) GameObject leaderObj = FindNearestLemoning(leaders, lemonObj.transform.position); // Set lemon to follow LemoningController lemon = lemonObj.GetComponent <LemoningController>(); lemon.SetFollow(leaderObj); lemonings.Remove(lemonObj); leaders.Add(lemonObj); } }
/// Attempt to move the lemoning to a new location private void AttemptMove() { for (int i = 0; i < moveAttempts; i++) { // First, pick a point nearby to the player Vector2 circle = Random.insideUnitCircle * maximumMoveDistance; Vector3 target = transform.position + new Vector3(circle.x, 0.0f, circle.y); // Next, find point on Navmesh NavMeshHit navMeshHit; if (NavMesh.SamplePosition(target, out navMeshHit, 3.0f, NavMesh.AllAreas)) { // If point is far enough from player, use it if (Vector3.Distance(navMeshHit.position, transform.position) >= minimumMoveDistance) { m_Controller.SetDestination(navMeshHit.position); return; } } } }