Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class FlockAgent : MonoBehaviour
- {
- public int ID = 0;
- public bool SeperationOn = true;
- public bool CohesionOn = true;
- public bool AllignmentOn = true;
- public float SeperationWeight = 1;
- public float CohesionWeight = 1;
- public float AllignmentWeight = 1;
- public float SeperationRadius = 20;
- public float CohesionRadius = 15;
- public float CohesionOffset = 10;
- public float AllignmentOffset = 15;
- public float AllignmentRadius = 25;
- public float SeperationForce = 10;
- public float CohesionForce = 7;
- public float AllignmentForce = 6;
- public Transform MyTransform;
- public List <FlockAgent> Neighbours;
- public bool Is2D = true;
- public Vector3 Velocity;
- Vector3 TempV = Vector3.zero;
- Vector3 TempVV = Vector3.zero;
- public float MaxSpeed = 30;
- public float MinSpeed = 10;
- Color ToColor;
- // Use this for initialization
- void Start ()
- {
- MyTransform = transform;
- Velocity = new Vector3(Random.Range( -MaxSpeed,MaxSpeed),0,Random.Range( -MaxSpeed,MaxSpeed));
- }
- // Update is called once per frame
- void Update ()
- {
- for (int i = 0; i < Neighbours.Count; ++i)
- {
- int currentID = Neighbours[i].ID;
- for (int ii = i+1; ii < Neighbours.Count; ++ii)
- {
- if (currentID == Neighbours[ii].ID)
- {
- Neighbours.RemoveAt(ii);
- ii--;
- }
- }
- }
- TempV = Vector3.zero;
- TempV += Seperation() * SeperationWeight;
- TempV += Allignment() * AllignmentWeight;
- TempV += Cohesion() * CohesionWeight;
- TempV *= 0.333f;
- Velocity += TempV;
- TempV *= 0.5f;
- if (Velocity.sqrMagnitude < ((MinSpeed) * (MinSpeed)))
- {
- Velocity = Velocity.normalized * MinSpeed;
- }
- else if (Velocity.sqrMagnitude > ((MaxSpeed) * (MaxSpeed)))
- {
- Velocity = Velocity.normalized * MaxSpeed;
- }
- if (!(Velocity.x == 0 && Velocity.z == 0))
- {
- MyTransform.position += Velocity * Time.deltaTime;
- MyTransform.forward = Velocity;
- Velocity *= 0.9f;
- }
- ToColor = Color.Lerp(Color.red, Color.cyan, (Neighbours.Count * 0.02f));
- foreach (Renderer RR in GetComponentsInChildren<Renderer>())
- {
- RR.material.color = Color.Lerp(RR.material.color,ToColor,0.3f);
- }
- TempV = Vector3.one;
- TempV.z = Mathf.Lerp(1.5f, 2.5f, Velocity.sqrMagnitude / ((MaxSpeed) * (MaxSpeed)));
- TempV *= 0.2f+ ((1-(CohCount * 0.02f))*0.8f);
- MyTransform.localScale = TempV;
- }
- Vector3 Seperation()
- {
- TempV = Vector3.zero;
- for (int i = 0; i < Neighbours.Count; ++i)
- {
- if (CircleCollsison(MyTransform.position, SeperationRadius,
- Neighbours[i].MyTransform.position, 0))
- {
- TempVV = MyTransform.position - Neighbours[i].MyTransform.position;
- float DistPower = TempVV.sqrMagnitude / (SeperationRadius * SeperationRadius);
- TempVV = TempVV.normalized * (1-DistPower);
- TempV += TempVV;
- }
- }
- TempV = TempV.normalized * SeperationForce;
- return TempV;
- }
- int CohCount = 0;
- Vector3 Cohesion()
- {
- TempV = Vector3.zero;
- int AverageTot = 0;
- CohCount = 0;
- for (int i = 0; i < Neighbours.Count; ++i)
- {
- if (CircleCollsison(MyTransform.position+ (MyTransform.forward * CohesionOffset), CohesionRadius,
- Neighbours[i].MyTransform.position, 0))
- {
- TempV += Neighbours[i].MyTransform.position;
- AverageTot++;
- CohCount++;
- }
- }
- TempV /= AverageTot;
- TempV = TempV - MyTransform.position;
- TempV = TempV.normalized * CohesionForce;
- return TempV;
- }
- Vector3 Allignment()
- {
- TempV = Vector3.zero;
- int AverageTot = 0;
- for (int i = 0; i < Neighbours.Count; ++i)
- {
- if (CircleCollsison(MyTransform.position + (MyTransform.forward * AllignmentOffset), AllignmentRadius,
- Neighbours[i].MyTransform.position, 0))
- {
- TempV += Neighbours[i].Velocity;
- AverageTot++;
- }
- }
- TempV /= AverageTot;
- TempV = TempV.normalized * AllignmentForce;
- return TempV;
- }
- public float GetMaxRange()
- {
- return (SeperationRadius > CohesionRadius) ? ((SeperationRadius > AllignmentRadius) ? SeperationRadius : AllignmentRadius) : (CohesionRadius > AllignmentRadius) ? CohesionRadius : AllignmentRadius;
- }
- public void ResetGroup()
- {
- Neighbours = new List<FlockAgent>();
- }
- bool CircleCollsison(Vector3 P1, float R1,Vector3 P2, float R2)
- {
- return (P1 - P2).sqrMagnitude < ((R1+R2)*(R1+R2));
- }
- void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.blue;
- Gizmos.DrawWireSphere (transform.position + (transform.forward * CohesionOffset), CohesionRadius);
- Gizmos.color = Color.green;
- Gizmos.DrawWireSphere (transform.position+ (transform.forward * AllignmentOffset), AllignmentRadius);
- Gizmos.color = Color.red;
- Gizmos.DrawWireSphere (transform.position, SeperationRadius);
- }
- }
Add Comment
Please, Sign In to add comment