/// <summary> /// Get crowd average velocity /// </summary> /// <returns>Crowd average velocity</returns> private bool GetCrowdAverageVelocity(out Vector3 _AverageVelocity) { List <SteeringCrowdUnit> crowd = SteeringCrowdUnit.CrowdUnitList; List <SteeringCrowdUnit> crowdNeighbors = new List <SteeringCrowdUnit>(); float sqrDist = 0; _AverageVelocity = Vector3.zero; // Get all crow units in neighbor hood for (int i = 0; i < crowd.Count; i++) { SteeringCrowdUnit crowdUnit = crowd[i]; if (crowdUnit != null) { if (crowdUnit.gameObject != gameObject) { sqrDist = (crowdUnit.transform.position - transform.position).sqrMagnitude; if (sqrDist < m_NeighborHoodRadius * m_NeighborHoodRadius) { crowdNeighbors.Add(crowdUnit); } } } } if (crowdNeighbors.Count < m_MinNeighborHoodUnitCount) { return(false); } // Get average velocity of neighbor hood for (int i = 0; i < crowdNeighbors.Count; i++) { if (!m_AdaptSpeedToCrowdSpeed) { _AverageVelocity += crowdNeighbors[i].Orientation; } else { _AverageVelocity += crowdNeighbors[i].Velocity; } } _AverageVelocity /= crowdNeighbors.Count; if (!m_AdaptSpeedToCrowdSpeed) { _AverageVelocity = _AverageVelocity.normalized * SteeringCore.MaxSpeed; } return(true); }
/// <summary> /// Get separation velocity from crowd /// </summary> /// <returns>Separation velocity from crowd</returns> private bool GetSeparationVelocity(out Vector3 _SeparationForce) { List <SteeringCrowdUnit> crowd = SteeringCrowdUnit.CrowdUnitList; List <SteeringCrowdUnit> crowdNeighbors = new List <SteeringCrowdUnit>(); float sqrDist = 0; _SeparationForce = Vector3.zero; // Get all crow units in neighbor hood for (int i = 0; i < crowd.Count; i++) { SteeringCrowdUnit crowdUnit = crowd[i]; if (crowdUnit != null) { if (crowdUnit.gameObject != gameObject) { sqrDist = (crowdUnit.transform.position - transform.position).sqrMagnitude; if (sqrDist < m_NeighborHoodRadius * m_NeighborHoodRadius) { crowdNeighbors.Add(crowdUnit); } } } } if (crowdNeighbors.Count < m_MinNeighborHoodUnitCount) { return(false); } Vector3 force; // Calculate separation from neighbor hood for (int i = 0; i < crowdNeighbors.Count; i++) { force = transform.position - crowdNeighbors[i].transform.position; force *= 1 - Mathf.Min(force.sqrMagnitude / (m_NeighborHoodRadius * m_NeighborHoodRadius), 1); _SeparationForce += force; } _SeparationForce /= crowdNeighbors.Count; return(true); }
/// <summary> /// Get center of mass between all neighbor crowd units in neighborhood for cohesion positioning /// </summary> /// <returns>Center of mass between all neighbor crowd units in neighborhood</returns> private bool GetCohesionPoint(out Vector3 _CohesionPoint, out Vector3 _AverageVelocity) { List <SteeringCrowdUnit> crowd = SteeringCrowdUnit.CrowdUnitList; List <SteeringCrowdUnit> crowdNeighbors = new List <SteeringCrowdUnit>(); float sqrDist = 0; _CohesionPoint = Vector3.zero; _AverageVelocity = Vector3.zero; // Get all crow units in neighbor hood for (int i = 0; i < crowd.Count; i++) { SteeringCrowdUnit crowdUnit = crowd[i]; if (crowdUnit != null) { if (crowdUnit.gameObject != gameObject) { sqrDist = (crowdUnit.transform.position - transform.position).sqrMagnitude; if (sqrDist < m_NeighborHoodRadius * m_NeighborHoodRadius) { crowdNeighbors.Add(crowdUnit); } } } } if (crowdNeighbors.Count < m_MinNeighborHoodUnitCount) { return(false); } // Get center of mass in neighbor hood for (int i = 0; i < crowdNeighbors.Count; i++) { _CohesionPoint += crowdNeighbors[i].transform.position; _AverageVelocity += crowdNeighbors[i].Velocity; } _CohesionPoint /= crowdNeighbors.Count; _AverageVelocity /= crowdNeighbors.Count; return(true); }