Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class vectorboidsscript : MonoBehaviour
- {
- /*
- The simulation works based on vectors. every starling will have the "starling" tag in unity.
- They will also have a public vector "veltomove". that's the vector they will move and where they face
- Also do note that this script is just for one boid starling. you will need to spawn multiple
- instances of the object with this script attached.
- I've also added an extra goal object to control where the flock simulation to go
- Every frame the starlings go through this loop
- - reset data/variables
- - check distance with every starling to get nearby neighbours
- - calculate average rotation (by vectors)
- - calculate average position
- - calculate direction to avoid collisions
- - calculate random vector direction
- - calculate vector towards goal object
- - average all movement vectors
- - lerp current facing vector to the new averaged vector
- - apply movement to starling
- */
- //Movement vectors
- public Vector3 veltomove;
- Vector3 intendedvel;
- //Goal variable
- Transform goal;
- // all Starlings Array
- GameObject[] allStarlings;
- //How far do you want the starlings to see
- float neighbourRad = 20f;
- float avoidRad = 5f;
- //Lists of current neighbours
- List<GameObject> neighboursList;
- List<GameObject> avoidList;
- //list of vectors of current neighbours and their final averaged vectors
- List<Vector3> cohesionVelsList;
- Vector3 cohesionVel;
- List<Vector3> avoidVelsList;
- Vector3 avoidVel;
- List<Vector3> centreVelsList;
- Vector3 CentreVel;
- //noise variables
- float noise = 1f;
- Vector3 noiseVel;
- //vector aiming at goal
- Vector3 goalVel;
- //starling variables
- float speed = 60f;
- float speedRandom=0.9f;
- float turnSpeed = 1f;
- //this is just for my Animator
- Animator anim;
- // Start is called before the first frame update
- void Start()
- {
- /*
- At the start it will:
- -pick a random vector to Movement
- -calculate a random speed
- -set the goal Goal transform
- -start the animations
- -multiply key measurements with the local scale of object
- */
- veltomove= new Vector3(Random.Range(-1f, 1f),Random.Range(-1f, 1f),Random.Range(-1f, 1f));
- speed+=Random.Range(speed*(1-speedRandom),speed*(1+speedRandom));
- goal=GameObject.Find("Goal").transform;
- //anim stuff
- anim=transform.GetChild(0).GetComponent<Animator>();
- StartCoroutine("AnimStart");
- ScaleMeasurements();
- }
- // Update is called once per frame
- void Update()
- {
- ResetData();
- GetNeighbours();
- CalCohesionVel();
- CalCentreVel();
- CalAvoidVel();
- NoiseVel();
- GoalVel();
- CollateVels();
- RotateMesh();
- MoveForward();
- }
- IEnumerator AnimStart()
- {
- yield return new WaitForSeconds(Random.Range(0f,1f));
- anim.SetTrigger("flap");
- }
- //---------------------------------------
- void ScaleMeasurements()
- {
- float scale=transform.localScale.y;
- speed*=scale;
- neighbourRad*=scale;
- avoidRad*=scale;
- }
- void ResetData()
- {
- neighboursList=new List<GameObject>();
- avoidList=new List<GameObject>();
- cohesionVelsList=new List<Vector3>();
- cohesionVel=Vector3.zero;
- avoidVelsList=new List<Vector3>();
- avoidVel=Vector3.zero;
- centreVelsList=new List<Vector3>();
- CentreVel=Vector3.zero;
- nearbyObs=new List<GameObject>();
- obsVel=Vector3.zero;
- }
- void GetNeighbours()
- {
- allStarlings=GameObject.FindGameObjectsWithTag("starling");
- foreach(GameObject i in allStarlings)
- {
- if(Vector3.Distance(transform.position, i.transform.position)<neighbourRad)
- {
- if (i != gameObject)
- {
- neighboursList.Add(i);
- }
- }
- }
- foreach(GameObject i in neighboursList)
- {
- if (Vector3.Distance(i.transform.position, transform.position)<avoidRad)
- {
- if (i != gameObject)
- {
- avoidList.Add(i);
- }
- }
- }
- }
- void CalCohesionVel()
- {
- if (neighboursList.Count>0)
- {
- Vector3 total = Vector3.zero;
- foreach(GameObject i in neighboursList)
- {
- cohesionVelsList.Add(GetComponent<vectorboidsscript>().veltomove);
- }
- foreach(Vector3 i in cohesionVelsList)
- {
- total+=i;
- }
- cohesionVel=total/cohesionVelsList.Count;
- }
- }
- void CalCentreVel()
- {
- if (neighboursList.Count>0)
- {
- Vector3 total = Vector3.zero;
- foreach(GameObject i in neighboursList)
- {
- centreVelsList.Add(i.transform.position);
- }
- foreach(Vector3 i in centreVelsList)
- {
- total+=i;
- }
- CentreVel=total/centreVelsList.Count;
- }
- }
- void CalAvoidVel()
- {
- if (avoidList.Count>0)
- {
- Vector3 total = Vector3.zero;
- foreach(GameObject i in avoidList)
- {
- avoidVelsList.Add(transform.position-i.transform.position);
- }
- foreach(Vector3 i in avoidVelsList)
- {
- total+=i;
- }
- avoidVel=total/avoidVelsList.Count;
- }
- }
- void NoiseVel()
- {
- noiseVel = new Vector3(Random.Range(-noise, noise),Random.Range(-noise, noise),Random.Range(-noise, noise));
- }
- void GoalVel()
- {
- goalVel=goal.transform.position-transform.position;
- }
- void CollateVels()
- {
- Vector3 coh=cohesionVel*3f;
- Vector3 av=avoidVel;
- Vector3 cen=CentreVel.normalized*0.5f;
- Vector3 n = noiseVel;
- Vector3 g = goalVel*0.6f;
- intendedvel=(coh+cen+g+n).normalized+(av.normalized*1);
- }
- void RotateMesh()
- {
- Transform mesh = transform.GetChild(0);
- transform.LookAt(veltomove+transform.position);
- }
- void MoveForward()
- {
- veltomove=Vector3.Lerp(veltomove, intendedvel, turnSpeed*Time.deltaTime);
- transform.Translate((veltomove*speed*Time.deltaTime));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment