// Start is called before the first frame update void Start() { for (int i = 0; i < numBirds; i++) { GameObject GO = Instantiate(bird.gameObject, this.transform); GO.transform.localPosition = Random.onUnitSphere; BirdMovement_Old birdMove = GO.GetComponent <BirdMovement_Old>(); birdMove.targetDirection = Random.onUnitSphere; birdMove.enabled = true; TrailRenderer trail = GO.GetComponentInChildren <TrailRenderer>(); trail.startColor = Random.ColorHSV(0, 1f, .9f, 1f, 0.9f, 1.0f, .99f, 1.0f); trail.endColor = new Color(0, 0, 0, 0f); Light ll = GO.GetComponentInChildren <Light>(); ll.color = trail.startColor; } }
// This class is meant to purely compute the next location the bird object should fly towards. public static Vector3 GetNewMovement(BirdMovement_Old bm) { currentBirdsLoc[bm] = bm.transform.position; // First lets search to see if any points are free Quaternion qq = bm.transform.rotation; int __numRays = _numRays; float __searching = _searching; Vector3 selection = qq * Quaternion.Inverse(bm.transform.localRotation) * bm.targetDirection; float golden = (1 + Mathf.Sqrt(5f)); bool dodging = false; for (int i = 0; i < __numRays; i++) { float phi = Mathf.Acos(1f - 2f * (i + .5f) / (_numRays + .5f)); float theta = Mathf.PI * golden * (i + .5f); Vector3 temp = qq * new Vector3(Mathf.Cos(theta) * Mathf.Sin(phi), Mathf.Sin(theta) * Mathf.Sin(phi), Mathf.Cos(phi)); //Debug.DrawRay(bm.transform.position, temp, Color.blue); if (phi > _searching) { break; } else if (Physics.SphereCast(new Ray(bm.transform.position, temp), _sphereCheckRad, bm.speed * Time.smoothDeltaTime * 2f, _targetLayer)) { selection -= temp; dodging = true; } } if (dodging) { return(bm.transform.localRotation * Quaternion.Inverse(qq) * selection.normalized); } Vector3 aveflock = bm.transform.position; bool seenOthers = false; int numberSeen = 1; Vector3 aveVel = selection; foreach (BirdMovement_Old bmm in otherBirds) { if (!bmm.Equals(bm)) { float distanceToOther = Vector3.Distance(bmm.transform.position, bm.transform.position); if (distanceToOther < bm.flockRange) { seenOthers = true; aveflock += bmm.transform.position; numberSeen++; // But like...yeah if (distanceToOther < _dodgeDist) { selection -= (bmm.transform.position - bm.transform.position) * _ruleStrengths.x; } aveVel += bmm.transform.forward; } } } if (seenOthers) { //Debug.DrawLine(bm.transform.position, (aveflock/numberSeen), Color.red); selection += ((aveflock / numberSeen) - bm.transform.position).normalized * _ruleStrengths.z; selection += (aveVel / numberSeen).normalized * _ruleStrengths.y; } return(bm.transform.localRotation * Quaternion.Inverse(qq) * selection.normalized); }
public static void RegisterBird(BirdMovement_Old bm) { otherBirds.Add(bm); currentBirdsLoc.Add(bm, bm.transform.position); }