Advertisement
Marsh_Mello

PipeController

Apr 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5.  
  6. public class PipeController : MonoBehaviour
  7. {
  8. public GameObject[] waypoints, pipeModels;
  9.  
  10. private void Update()
  11. {
  12. for (int i = 0; i < waypoints.Length; i++)
  13. {
  14. waypoints[i].SetActive(pipeModels[i].activeInHierarchy);
  15. }
  16. }
  17.  
  18. private void OnTriggerStay(Collider other)
  19. {
  20. if (other.gameObject.tag == "Item")
  21. {
  22. ItemController itemController = other.gameObject.GetComponent<ItemController>();
  23. if (itemController.currentPipe == transform.parent.gameObject)
  24. {
  25. if (itemController.beforeCenter)
  26. {
  27. float step = itemController.currentSpeed * Time.deltaTime;
  28. other.transform.position =
  29. Vector3.MoveTowards(other.transform.position,
  30. new Vector3(transform.position.x , transform.position.y - 5 , transform.position.z), step);
  31. }
  32. else
  33. {
  34. float step = itemController.currentSpeed * Time.deltaTime;
  35. other.transform.position =
  36. Vector3.MoveTowards(other.transform.position,
  37. new Vector3(itemController.exitWaypoint.transform.position.x , itemController.exitWaypoint.transform.position.y - 5 , itemController.exitWaypoint.transform.position.z), step);
  38. }
  39.  
  40. if (other.transform.position ==
  41. new Vector3(transform.position.x, transform.position.y - 5, transform.position.z))
  42. {
  43. PickRandomWaypoint(other.gameObject);
  44. }
  45. }
  46. }
  47. }
  48.  
  49. void PickRandomWaypoint(GameObject other)
  50. {
  51.  
  52. ItemController itemController = other.GetComponent<ItemController>();
  53. int amountInactive = 0;
  54. for (int i = 0; i < waypoints.Length; i++)
  55. {
  56. if (!waypoints[i].activeInHierarchy && waypoints[i] != itemController.entranceWaypoint)
  57. {
  58. amountInactive++;
  59. }
  60. }
  61.  
  62.  
  63. if (amountInactive == 5)
  64. {
  65. Destroy(other);
  66. }
  67.  
  68. int randomWaypoint = Random.Range(0, 6);
  69. if (waypoints[randomWaypoint].active == false)
  70. {
  71. PickRandomWaypoint(other);
  72. return;
  73. }
  74.  
  75. if (waypoints[randomWaypoint] == other.GetComponent<ItemController>().entranceWaypoint)
  76. {
  77. PickRandomWaypoint(other);
  78. return;
  79. }
  80.  
  81. other.GetComponent<ItemController>().exitWaypoint = waypoints[randomWaypoint];
  82. other.GetComponent<ItemController>().beforeCenter = false;
  83.  
  84. }
  85.  
  86. public void UpdatePipes(int direction)
  87. {
  88. pipeModels[direction - 1].SetActive(true);
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement