コード例 #1
0
    /// <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 neighborhood
        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);
    }
コード例 #2
0
 void Start()
 {
     animator = GetComponentInChildren <Animator>();
     unit     = GetComponent <SteeringCrowdUnit>();
 }