Ejemplo n.º 1
0
        /// <summary>
        /// Average velocity
        /// </summary>
        /// <returns></returns>
        Vector3 Align()
        {
            Vector3 sum   = Vector3.zero;
            int     count = 0;

            foreach (var agent in _agentManager.Crowd)
            {
                //Don't include self in calculation
                if (agent == gameObject)
                {
                    continue;
                }
                //if (agent.transform.position == _leaderPosition) continue;

                Agent other    = agent.GetComponent <Agent>();
                float distance = Vector3.Distance(_position, other.GetPosition());
                //If the other agent is within distance then include their position for consideration when flocking
                if (!(distance < _agentManager.NeighbourDistance))
                {
                    continue;
                }
                sum += other.GetVelocity();
                count++;
            }

            // If there are no neighbors in the vicinity return a zero vector
            if (count <= 0)
            {
                return(Vector3.zero);
            }

            // Get average velocity
            sum /= count;
            Vector3 steer = sum - _velocity;

            return(steer);
        }