Wolvenspud

FlockAgent - FlockAgent

Nov 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FlockAgent : MonoBehaviour
  6. {
  7.  
  8.     public int ID = 0;
  9.  
  10.     public bool SeperationOn = true;
  11.     public bool CohesionOn = true;
  12.     public bool AllignmentOn = true;
  13.  
  14.     public float SeperationWeight = 1;
  15.     public float CohesionWeight = 1;
  16.     public float AllignmentWeight = 1;
  17.  
  18.  
  19.     public float SeperationRadius = 20;
  20.     public float CohesionRadius = 15;
  21.     public float CohesionOffset = 10;
  22.     public float AllignmentOffset = 15;
  23.     public float AllignmentRadius = 25;
  24.  
  25.  
  26.     public float SeperationForce = 10;
  27.     public float CohesionForce = 7;
  28.     public float AllignmentForce = 6;
  29.  
  30.     public Transform MyTransform;
  31.     public List <FlockAgent> Neighbours;
  32.  
  33.     public bool Is2D = true;
  34.  
  35.  
  36.     public Vector3 Velocity;
  37.     Vector3 TempV = Vector3.zero;
  38.     Vector3 TempVV = Vector3.zero;
  39.  
  40.  
  41.     public float MaxSpeed = 30;
  42.     public float MinSpeed = 10;
  43.  
  44.     Color ToColor;
  45.     // Use this for initialization
  46.     void Start ()
  47.     {
  48.         MyTransform = transform;
  49.         Velocity = new Vector3(Random.Range( -MaxSpeed,MaxSpeed),0,Random.Range( -MaxSpeed,MaxSpeed));
  50.     }
  51.    
  52.     // Update is called once per frame
  53.     void Update ()
  54.     {
  55.  
  56.         for (int i = 0; i < Neighbours.Count; ++i)
  57.         {
  58.             int currentID = Neighbours[i].ID;
  59.             for (int ii = i+1; ii < Neighbours.Count; ++ii)
  60.             {
  61.                 if (currentID == Neighbours[ii].ID)
  62.                 {
  63.                     Neighbours.RemoveAt(ii);
  64.                     ii--;
  65.                 }
  66.             }
  67.         }
  68.         TempV = Vector3.zero;
  69.         TempV += Seperation() * SeperationWeight;
  70.         TempV += Allignment() * AllignmentWeight;
  71.         TempV += Cohesion() * CohesionWeight;
  72.         TempV *= 0.333f;
  73.          
  74.         Velocity += TempV;
  75.         TempV *= 0.5f;
  76.            
  77.         if (Velocity.sqrMagnitude < ((MinSpeed) * (MinSpeed)))
  78.         {
  79.             Velocity = Velocity.normalized * MinSpeed;
  80.         }
  81.         else if (Velocity.sqrMagnitude > ((MaxSpeed) * (MaxSpeed)))
  82.         {
  83.             Velocity = Velocity.normalized * MaxSpeed;
  84.         }
  85.         if (!(Velocity.x == 0 && Velocity.z == 0))
  86.         {
  87.             MyTransform.position += Velocity * Time.deltaTime;
  88.             MyTransform.forward = Velocity;
  89.             Velocity *= 0.9f;
  90.         }
  91.  
  92.         ToColor = Color.Lerp(Color.red, Color.cyan, (Neighbours.Count * 0.02f));
  93.         foreach (Renderer RR in GetComponentsInChildren<Renderer>())
  94.         {
  95.             RR.material.color = Color.Lerp(RR.material.color,ToColor,0.3f);
  96.         }
  97.  
  98.         TempV = Vector3.one;
  99.         TempV.z = Mathf.Lerp(1.5f, 2.5f, Velocity.sqrMagnitude / ((MaxSpeed) * (MaxSpeed)));
  100.         TempV *= 0.2f+ ((1-(CohCount * 0.02f))*0.8f);
  101.         MyTransform.localScale = TempV;
  102.     }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.     Vector3 Seperation()
  114.     {
  115.         TempV = Vector3.zero;
  116.         for (int i = 0; i < Neighbours.Count; ++i)
  117.         {
  118.             if (CircleCollsison(MyTransform.position, SeperationRadius,
  119.                 Neighbours[i].MyTransform.position, 0))
  120.             {
  121.                 TempVV = MyTransform.position - Neighbours[i].MyTransform.position;
  122.                 float DistPower = TempVV.sqrMagnitude / (SeperationRadius * SeperationRadius);
  123.                 TempVV = TempVV.normalized * (1-DistPower);
  124.                     TempV += TempVV;
  125.             }
  126.         }
  127.  
  128.         TempV =  TempV.normalized * SeperationForce;
  129.         return TempV;
  130.     }
  131.     int CohCount = 0;
  132.     Vector3 Cohesion()
  133.     {
  134.         TempV = Vector3.zero;
  135.         int AverageTot = 0;
  136.         CohCount = 0;
  137.         for (int i = 0; i < Neighbours.Count; ++i)
  138.         {
  139.             if (CircleCollsison(MyTransform.position+ (MyTransform.forward * CohesionOffset), CohesionRadius,
  140.                 Neighbours[i].MyTransform.position, 0))
  141.             {
  142.                 TempV += Neighbours[i].MyTransform.position;
  143.                 AverageTot++;
  144.                 CohCount++;
  145.             }
  146.         }
  147.         TempV /= AverageTot;
  148.  
  149.         TempV = TempV - MyTransform.position;
  150.         TempV =  TempV.normalized * CohesionForce;
  151.         return TempV;
  152.     }
  153.  
  154.     Vector3 Allignment()
  155.     {
  156.         TempV = Vector3.zero;
  157.         int AverageTot = 0;
  158.         for (int i = 0; i < Neighbours.Count; ++i)
  159.         {
  160.             if (CircleCollsison(MyTransform.position + (MyTransform.forward * AllignmentOffset), AllignmentRadius,
  161.                 Neighbours[i].MyTransform.position, 0))
  162.             {
  163.                 TempV += Neighbours[i].Velocity;
  164.                 AverageTot++;
  165.             }
  166.         }
  167.         TempV /= AverageTot;
  168.  
  169.         TempV =  TempV.normalized * AllignmentForce;
  170.         return TempV;
  171.     }
  172.  
  173.  
  174.  
  175.     public float GetMaxRange()
  176.     {
  177.         return (SeperationRadius > CohesionRadius) ? ((SeperationRadius > AllignmentRadius) ? SeperationRadius : AllignmentRadius) : (CohesionRadius >  AllignmentRadius) ? CohesionRadius : AllignmentRadius;
  178.     }
  179.  
  180.     public void ResetGroup()
  181.     {
  182.         Neighbours = new List<FlockAgent>();
  183.     }
  184.     bool CircleCollsison(Vector3 P1, float R1,Vector3 P2, float R2)
  185.     {
  186.         return (P1 - P2).sqrMagnitude < ((R1+R2)*(R1+R2));
  187.     }
  188.  
  189.     void OnDrawGizmosSelected()
  190.     {
  191.         Gizmos.color = Color.blue;
  192.         Gizmos.DrawWireSphere (transform.position + (transform.forward * CohesionOffset), CohesionRadius);
  193.  
  194.         Gizmos.color = Color.green;
  195.         Gizmos.DrawWireSphere (transform.position+ (transform.forward * AllignmentOffset), AllignmentRadius);
  196.  
  197.         Gizmos.color = Color.red;
  198.         Gizmos.DrawWireSphere (transform.position, SeperationRadius);
  199.  
  200.    }
  201. }
Add Comment
Please, Sign In to add comment