/// <summary> /// /// </summary> /// <param name="manager"></param> void Flocking(FlockManager manager) { Vector3 centre = Vector3.zero; Vector3 avoid = Vector3.zero; float fSpeed = 0.01f; float nDistance; int groupSize = 0; foreach (var a in manager.GetAllFlock()) { if (a == transform) { continue; } var agt = a.GetComponent <FlockAgent>(); nDistance = Vector3.Distance(a.transform.position, transform.position); if (nDistance <= manager._neighborDistance) { if (agt.GetType() == _type) { centre += a.transform.position; } else { centre += (transform.position - a.position); } groupSize++; if (nDistance < manager._avoidDistance) { avoid += (transform.position - a.position); } fSpeed += agt.GetSpeed(); } } if (groupSize > 0) { centre = (centre) / groupSize + (manager.GetGoalPos(this)); _speed = fSpeed / groupSize; Vector3 dir = (centre + avoid) - transform.position + (-toAvoid); if (dir != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), manager._flockSpeed * Time.deltaTime); //if (fDirection != 0) // direction = fDirection / Mathf.Abs(fDirection); flocking = true; Debug.DrawRay(transform.position, dir.normalized * manager._lookRange * 2f, Color.red); } } else { flocking = false; transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(-toAvoid), manager._flockSpeed * Time.deltaTime); } }