Advertisement
Guest User

Flocks

a guest
Jun 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. class Agent : MonoBehaviour
  2. {
  3.     public float alignmentWeight = 1.0f;
  4.     public float cohesionWeight = 1.0f;
  5.     public float seperationWeight = 1.0f;
  6.  
  7.     public float speed = 3.0f;
  8.  
  9.     public Vector3 Direction { get; private set; }
  10.  
  11.     private void Start()
  12.     {
  13.         Direction = UnityEngine.Random.insideUnitSphere;
  14.         FlocksManager.AddAgent(this);
  15.     }
  16.  
  17.     private void Update()
  18.     {
  19.         List<Agent> _neighbors = FlocksManager.GetNeighbors(this);
  20.  
  21.         //Calculate my final direction
  22.         Direction = Alignment(ref _neighbors) * alignmentWeight + Separation(ref _neighbors) * seperationWeight + Cohesion(ref _neighbors) * cohesionWeight;
  23.  
  24.         //Make it a radius of 1
  25.         Direction.Normalize();
  26.         transform.position += Direction * speed * Time.deltaTime;
  27.     }
  28.  
  29.     Vector3 Alignment(ref List<Agent> _neighbors)
  30.     {
  31.         Vector3 alignment = Vector3.zero;
  32.         for(int i = 0; i < _neighbors.Count; ++i)
  33.         {
  34.             alignment += _neighbors[i].Direction;
  35.         }
  36.  
  37.         return alignment / (float)_neighbors.Count;
  38.     }
  39.  
  40.     Vector3 Cohesion(ref List<Agent> _neighbors)
  41.     {
  42.         Vector3 cohesion = Vector3.zero;
  43.         for (int i = 0; i < _neighbors.Count; ++i)
  44.         {
  45.             cohesion += _neighbors[i].transform.position;
  46.         }
  47.  
  48.         return cohesion / (float)_neighbors.Count;
  49.     }
  50.  
  51.  
  52.     Vector3 Separation(ref List<Agent> _neighbors)
  53.     {
  54.         Vector3 cohesion = Vector3.zero;
  55.         for (int i = 0; i < _neighbors.Count; ++i)
  56.         {
  57.             cohesion -= _neighbors[i].transform.position;
  58.         }
  59.  
  60.         return cohesion / (float)_neighbors.Count;
  61.     }
  62. }
  63.  
  64. static class FlocksManager
  65. {
  66.     static float neighborRadius = 3.0f;
  67.     static List<Agent> agents = new List<Agent>();
  68.  
  69.     public static void AddAgent(Agent _agent)
  70.     {
  71.         agents.Add(_agent);
  72.     }
  73.  
  74.     public static void RemoveAgent(Agent _agent)
  75.     {
  76.         agents.Remove(_agent);
  77.     }
  78.  
  79.     public static List<Agent> GetNeighbors(Agent _agent)
  80.     {
  81.         List<Agent> neighbors = new List<Agent>();
  82.        
  83.         for(int i = 0; i < agents.Count; ++i)
  84.         {
  85.             if(Vector3.SqrMagnitude(agents[i].transform.position - _agent.transform.position) < neighborRadius * neighborRadius)
  86.             {
  87.                 neighbors.Add(agents[i]);
  88.             }
  89.         }
  90.  
  91.         return neighbors;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement