FlockScare.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using UnityEngine;
  2. using System.Collections;
  3. public class FlockScare : MonoBehaviour {
  4. public LandingSpotController[] landingSpotControllers; //List of landingspot controllers containing landingspots to scare
  5. public float scareInterval = 0.1f; //How often to check the next spot if it is close enough to scare (Every Nth second)
  6. public float distanceToScare = 2f; //How far from a landingspot this transform can call scare function
  7. public int checkEveryNthLandingSpot = 1; //Skip landingspots to move on to the next controller quicker (All spots will be included before the count restarts completely)
  8. public int InvokeAmounts = 1; //How many instances of Invokes to run (Only go above 1 when more controllers than the Interval can handle)
  9. //Can not be changed while game is running unless script is disabled then enabled
  10. private int lsc; //Counters used to keep track of what landingspots to check for distance
  11. private int ls;
  12. private LandingSpotController currentController;
  13. void CheckProximityToLandingSpots() {
  14. IterateLandingSpots();
  15. if (currentController._activeLandingSpots > 0 && CheckDistanceToLandingSpot(landingSpotControllers[lsc])) {
  16. landingSpotControllers[lsc].ScareAll();
  17. }
  18. Invoke("CheckProximityToLandingSpots", scareInterval);
  19. }
  20. //Counts trough all the landingspots inside all the controllers (For performance this is not done in a loop)
  21. void IterateLandingSpots() {
  22. ls += checkEveryNthLandingSpot;
  23. currentController = landingSpotControllers[lsc];
  24. int cc = currentController.transform.childCount;
  25. if (ls > cc - 1) {
  26. ls = ls - cc;
  27. if (lsc < landingSpotControllers.Length - 1)
  28. lsc++;
  29. else
  30. lsc = 0;
  31. }
  32. }
  33. //Checks distance to landingspots in the current landingspot controller
  34. bool CheckDistanceToLandingSpot(LandingSpotController lc) {
  35. Transform lcT = lc.transform;
  36. Transform lsT = lcT.GetChild(ls);
  37. LandingSpot lcSpot = lsT.GetComponent<LandingSpot>();
  38. if(lcSpot.landingChild != null){
  39. float d = (lsT.position - transform.position).sqrMagnitude; //Vector3.Distance(lcT.GetChild(ls).position, transform.position)
  40. if(d<distanceToScare*distanceToScare){
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. //Creates multiple instances of the Invoke that checks distance to landingspots
  47. void Invoker() {
  48. for (int i = 0; i < InvokeAmounts; i++){
  49. float s = (scareInterval / InvokeAmounts)*i;
  50. Invoke("CheckProximityToLandingSpots", scareInterval + s);
  51. }
  52. }
  53. //Starts checking for birds to scare when component is enabled
  54. void OnEnable() {
  55. CancelInvoke("CheckProximityToLandingSpots");
  56. if (landingSpotControllers.Length > 0)
  57. Invoker();
  58. #if UNITY_EDITOR
  59. else
  60. Debug.Log("Please assign LandingSpotControllers to FlockScare");
  61. #endif
  62. }
  63. //Stops checking for birds to scare when component is disabled
  64. void OnDisable() {
  65. CancelInvoke("CheckProximityToLandingSpots");
  66. }
  67. #if UNITY_EDITOR
  68. //Shows area of scaryness (Only used in editor)
  69. void OnDrawGizmos() {
  70. Gizmos.color = Color.red;
  71. Gizmos.DrawWireSphere(transform.position, distanceToScare);
  72. if (InvokeAmounts <= 0) InvokeAmounts = 1;
  73. }
  74. #endif
  75. }