public override Vector2 CalculateMove(BoidObject Boid, List <Transform> NeighbourBoids, BoidsManager AllBoids) { if (Weights.Length != Behaviours.Length) { Debug.LogError("WEIGHTS AND BEHAVIOURS ARE NOT THE SAME AMOUNTS"); return(Vector2.zero); } Vector2 Move = Vector2.zero; for (int i = 0; i < Behaviours.Length; i++) { Vector2 PartialMove = Behaviours[i].CalculateMove(Boid, NeighbourBoids, AllBoids) * Weights[i]; if (PartialMove != Vector2.zero) { if (PartialMove.sqrMagnitude > Weights[i] * Weights[i]) { PartialMove.Normalize(); PartialMove *= Weights[i]; } Move += PartialMove; } } return(Move); }
void Start() { boidObjs = new BoidObject[numBoids]; for (int i = 0; i < numBoids; i++) { boidObjs[i] = new BoidObject(boidPrefab, transform, playerControlScript); } }
public override Vector2 CalculateMove(BoidObject Boid, List <Transform> NeighbourBoids, BoidsManager AllBoids) { if (TargetTrans == Vector2.zero) { return(Vector2.zero); } Vector2 TargetMove = TargetTrans; return(TargetMove); }
public override List <Transform> Filter(BoidObject Boids, List <Transform> Original) { List <Transform> Filtered = new List <Transform>(); foreach (Transform Item in Original) { if (ObstacleMask == (ObstacleMask | (1 << Item.gameObject.layer))) { Filtered.Add(Item); } } return(Filtered); }
List <Transform> GetNearbyObjects(BoidObject Boid) { List <Transform> NeighbourObjects = new List <Transform>(); Collider2D[] NeighbourColliders = Physics2D.OverlapCircleAll(Boid.transform.position, NeighbourRadius); foreach (Collider2D Colliders in NeighbourColliders) { if (Colliders != Boid.BoidsCollider) { NeighbourObjects.Add(Colliders.transform); } } return(NeighbourObjects); }
private void Start() { SquareMaxSpeed = MaxMoveSpeed * MaxMoveSpeed; SquareNeighbourRadius = NeighbourRadius * NeighbourRadius; SquareAvoidanceRadius = SquareNeighbourRadius * AvoidanceRadius * AvoidanceRadius; for (int i = 0; i < BoidAmount; i++) { BoidObject NewBoid = Instantiate(BoidPrefab, Random.insideUnitCircle * BoidAmount * BoidDensity, Quaternion.Euler(Vector3.forward * Random.Range(0.0f, 360.0f)), this.transform); NewBoid.name = "Boid " + i; NewBoid.Initialize(this); Boids.Add(NewBoid); } }
public override Vector2 CalculateMove(BoidObject Boid, List <Transform> NeighbourBoids, BoidsManager AllBoids) { if (NeighbourBoids.Count == 0) { return(Boid.transform.up); } Vector2 AlignmentMove = Vector2.zero; foreach (Transform Item in NeighbourBoids) { AlignmentMove += (Vector2)Item.transform.up; } AlignmentMove /= NeighbourBoids.Count; return(AlignmentMove); }
public override Vector2 CalculateMove(BoidObject Boid, List <Transform> NeighbourBoids, BoidsManager AllBoids) { if (NeighbourBoids.Count == 0) { return(Vector2.zero); } Vector2 CohesionMove = Vector2.zero; foreach (Transform Item in NeighbourBoids) { CohesionMove += (Vector2)Item.position; } CohesionMove /= NeighbourBoids.Count; CohesionMove -= (Vector2)Boid.transform.position; return(CohesionMove); }
/// <summary> /// Creates and instantiates the boid objects. /// THe boids are spawned randomly around a 4x4x4 cube around the manager. /// </summary> private void _CreateBoids() { if (_managedBoids == null) { _managedBoids = new BoidObject[_boidCount]; } for (int i = 0; i < _boidCount; i++) { var prefab = _GetBoidPrefab(); var startPos = new Vector3(UnityEngine.Random.Range(-2f, 2f), UnityEngine.Random.Range(-2f, 2f), UnityEngine.Random.Range(-2f, 2f)); var boid = Instantiate(prefab, transform.position + startPos, Quaternion.identity); boid.AddComponent <BoidSubordinate> ().SetBoidManager(this); _managedBoids[i] = new BoidObject(boid); } if (_boidFollowCamera != null) { var folowCam = _boidFollowCamera.gameObject.AddComponent <BoidFollowCamera>(); folowCam.SetFollowTargetObject(_managedBoids[0].go); } }
public override Vector2 CalculateMove(BoidObject Boid, List <Transform> NeighbourBoids, BoidsManager AllBoids) { if (NeighbourBoids.Count == 0) { return(Vector2.zero); } Vector2 CohesionMove = Vector2.zero; List <Transform> FilterStuff = (Filter == null) ? NeighbourBoids : Filter.Filter(Boid, NeighbourBoids); foreach (Transform Item in FilterStuff) { CohesionMove += (Vector2)Item.position; } CohesionMove /= NeighbourBoids.Count; CohesionMove -= (Vector2)Boid.transform.position; if (float.IsNaN(CurrentVelocity.x) || float.IsNaN(CurrentVelocity.y)) { CurrentVelocity = Vector2.zero; } CohesionMove = Vector2.SmoothDamp(Boid.transform.up, CohesionMove, ref CurrentVelocity, BoidSmoothTime); return(CohesionMove); }
public override Vector2 CalculateMove(BoidObject Boid, List <Transform> NeighbourBoids, BoidsManager AllBoids) { if (NeighbourBoids.Count == 0) { return(Vector2.zero); } Vector2 SeparationMove = Vector2.zero; int AvoidObjects = 0; foreach (Transform Item in NeighbourBoids) { if (Vector2.SqrMagnitude(Item.position - Boid.transform.position) < AllBoids.SquareAvoidanceRadiusGet) { AvoidObjects++; SeparationMove += (Vector2)(Boid.transform.position - Item.position); } } if (AvoidObjects > 0) { SeparationMove /= AvoidObjects; } return(SeparationMove); }
public abstract Vector2 CalculateMove(BoidObject Boid, List <Transform> NeighbourBoids, BoidsManager AllBoids);
public abstract List <Transform> Filter(BoidObject Boids, List <Transform> Original);