elbert45

Boids Script

May 5th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class vectorboidsscript : MonoBehaviour
  6. {
  7. /*
  8. The simulation works based on vectors. every starling will have the "starling" tag in unity.
  9. They will also have a public vector "veltomove". that's the vector they will move and where they face
  10.  
  11. Also do note that this script is just for one boid starling. you will need to spawn multiple
  12. instances of the object with this script attached.
  13.  
  14. I've also added an extra goal object to control where the flock simulation to go
  15.  
  16. Every frame the starlings go through this loop
  17. - reset data/variables
  18. - check distance with every starling to get nearby neighbours
  19. - calculate average rotation (by vectors)
  20. - calculate average position
  21. - calculate direction to avoid collisions
  22. - calculate random vector direction
  23. - calculate vector towards goal object
  24. - average all movement vectors
  25. - lerp current facing vector to the new averaged vector
  26. - apply movement to starling
  27. */
  28.  
  29.  
  30. //Movement vectors
  31. public Vector3 veltomove;
  32. Vector3 intendedvel;
  33.  
  34. //Goal variable
  35. Transform goal;
  36.  
  37. // all Starlings Array
  38. GameObject[] allStarlings;
  39.  
  40. //How far do you want the starlings to see
  41. float neighbourRad = 20f;
  42. float avoidRad = 5f;
  43.  
  44. //Lists of current neighbours
  45. List<GameObject> neighboursList;
  46. List<GameObject> avoidList;
  47.  
  48. //list of vectors of current neighbours and their final averaged vectors
  49. List<Vector3> cohesionVelsList;
  50. Vector3 cohesionVel;
  51. List<Vector3> avoidVelsList;
  52. Vector3 avoidVel;
  53. List<Vector3> centreVelsList;
  54. Vector3 CentreVel;
  55.  
  56. //noise variables
  57. float noise = 1f;
  58. Vector3 noiseVel;
  59.  
  60. //vector aiming at goal
  61. Vector3 goalVel;
  62.  
  63. //starling variables
  64. float speed = 60f;
  65. float speedRandom=0.9f;
  66. float turnSpeed = 1f;
  67.  
  68. //this is just for my Animator
  69. Animator anim;
  70.  
  71. // Start is called before the first frame update
  72. void Start()
  73. {
  74. /*
  75. At the start it will:
  76. -pick a random vector to Movement
  77. -calculate a random speed
  78. -set the goal Goal transform
  79. -start the animations
  80. -multiply key measurements with the local scale of object
  81. */
  82.  
  83. veltomove= new Vector3(Random.Range(-1f, 1f),Random.Range(-1f, 1f),Random.Range(-1f, 1f));
  84. speed+=Random.Range(speed*(1-speedRandom),speed*(1+speedRandom));
  85. goal=GameObject.Find("Goal").transform;
  86.  
  87. //anim stuff
  88. anim=transform.GetChild(0).GetComponent<Animator>();
  89. StartCoroutine("AnimStart");
  90.  
  91. ScaleMeasurements();
  92. }
  93.  
  94. // Update is called once per frame
  95. void Update()
  96. {
  97. ResetData();
  98. GetNeighbours();
  99.  
  100. CalCohesionVel();
  101. CalCentreVel();
  102. CalAvoidVel();
  103. NoiseVel();
  104. GoalVel();
  105.  
  106. CollateVels();
  107. RotateMesh();
  108. MoveForward();
  109.  
  110. }
  111.  
  112. IEnumerator AnimStart()
  113. {
  114. yield return new WaitForSeconds(Random.Range(0f,1f));
  115. anim.SetTrigger("flap");
  116. }
  117.  
  118.  
  119. //---------------------------------------
  120.  
  121. void ScaleMeasurements()
  122. {
  123. float scale=transform.localScale.y;
  124. speed*=scale;
  125. neighbourRad*=scale;
  126. avoidRad*=scale;
  127. }
  128.  
  129. void ResetData()
  130. {
  131. neighboursList=new List<GameObject>();
  132. avoidList=new List<GameObject>();
  133.  
  134. cohesionVelsList=new List<Vector3>();
  135. cohesionVel=Vector3.zero;
  136. avoidVelsList=new List<Vector3>();
  137. avoidVel=Vector3.zero;
  138. centreVelsList=new List<Vector3>();
  139. CentreVel=Vector3.zero;
  140.  
  141. nearbyObs=new List<GameObject>();
  142. obsVel=Vector3.zero;
  143. }
  144.  
  145.  
  146. void GetNeighbours()
  147. {
  148. allStarlings=GameObject.FindGameObjectsWithTag("starling");
  149.  
  150. foreach(GameObject i in allStarlings)
  151. {
  152. if(Vector3.Distance(transform.position, i.transform.position)<neighbourRad)
  153. {
  154. if (i != gameObject)
  155. {
  156. neighboursList.Add(i);
  157. }
  158. }
  159. }
  160.  
  161.  
  162. foreach(GameObject i in neighboursList)
  163. {
  164. if (Vector3.Distance(i.transform.position, transform.position)<avoidRad)
  165. {
  166. if (i != gameObject)
  167. {
  168. avoidList.Add(i);
  169. }
  170.  
  171. }
  172. }
  173. }
  174.  
  175. void CalCohesionVel()
  176. {
  177. if (neighboursList.Count>0)
  178. {
  179. Vector3 total = Vector3.zero;
  180. foreach(GameObject i in neighboursList)
  181. {
  182. cohesionVelsList.Add(GetComponent<vectorboidsscript>().veltomove);
  183. }
  184. foreach(Vector3 i in cohesionVelsList)
  185. {
  186. total+=i;
  187. }
  188. cohesionVel=total/cohesionVelsList.Count;
  189. }
  190. }
  191.  
  192. void CalCentreVel()
  193. {
  194. if (neighboursList.Count>0)
  195. {
  196. Vector3 total = Vector3.zero;
  197. foreach(GameObject i in neighboursList)
  198. {
  199. centreVelsList.Add(i.transform.position);
  200. }
  201. foreach(Vector3 i in centreVelsList)
  202. {
  203. total+=i;
  204. }
  205. CentreVel=total/centreVelsList.Count;
  206. }
  207. }
  208.  
  209. void CalAvoidVel()
  210. {
  211. if (avoidList.Count>0)
  212. {
  213. Vector3 total = Vector3.zero;
  214. foreach(GameObject i in avoidList)
  215. {
  216. avoidVelsList.Add(transform.position-i.transform.position);
  217. }
  218. foreach(Vector3 i in avoidVelsList)
  219. {
  220. total+=i;
  221. }
  222. avoidVel=total/avoidVelsList.Count;
  223. }
  224. }
  225.  
  226. void NoiseVel()
  227. {
  228. noiseVel = new Vector3(Random.Range(-noise, noise),Random.Range(-noise, noise),Random.Range(-noise, noise));
  229. }
  230.  
  231. void GoalVel()
  232. {
  233. goalVel=goal.transform.position-transform.position;
  234. }
  235.  
  236. void CollateVels()
  237. {
  238. Vector3 coh=cohesionVel*3f;
  239. Vector3 av=avoidVel;
  240. Vector3 cen=CentreVel.normalized*0.5f;
  241. Vector3 n = noiseVel;
  242. Vector3 g = goalVel*0.6f;
  243. intendedvel=(coh+cen+g+n).normalized+(av.normalized*1);
  244. }
  245.  
  246. void RotateMesh()
  247. {
  248. Transform mesh = transform.GetChild(0);
  249. transform.LookAt(veltomove+transform.position);
  250. }
  251.  
  252. void MoveForward()
  253. {
  254. veltomove=Vector3.Lerp(veltomove, intendedvel, turnSpeed*Time.deltaTime);
  255. transform.Translate((veltomove*speed*Time.deltaTime));
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment