Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- public class PipeController : MonoBehaviour
- {
- public GameObject[] waypoints, pipeModels;
- private void Update()
- {
- for (int i = 0; i < waypoints.Length; i++)
- {
- waypoints[i].SetActive(pipeModels[i].activeInHierarchy);
- }
- }
- private void OnTriggerStay(Collider other)
- {
- if (other.gameObject.tag == "Item")
- {
- ItemController itemController = other.gameObject.GetComponent<ItemController>();
- if (itemController.currentPipe == transform.parent.gameObject)
- {
- if (itemController.beforeCenter)
- {
- float step = itemController.currentSpeed * Time.deltaTime;
- other.transform.position =
- Vector3.MoveTowards(other.transform.position,
- new Vector3(transform.position.x , transform.position.y - 5 , transform.position.z), step);
- }
- else
- {
- float step = itemController.currentSpeed * Time.deltaTime;
- other.transform.position =
- Vector3.MoveTowards(other.transform.position,
- new Vector3(itemController.exitWaypoint.transform.position.x , itemController.exitWaypoint.transform.position.y - 5 , itemController.exitWaypoint.transform.position.z), step);
- }
- if (other.transform.position ==
- new Vector3(transform.position.x, transform.position.y - 5, transform.position.z))
- {
- PickRandomWaypoint(other.gameObject);
- }
- }
- }
- }
- void PickRandomWaypoint(GameObject other)
- {
- ItemController itemController = other.GetComponent<ItemController>();
- int amountInactive = 0;
- for (int i = 0; i < waypoints.Length; i++)
- {
- if (!waypoints[i].activeInHierarchy && waypoints[i] != itemController.entranceWaypoint)
- {
- amountInactive++;
- }
- }
- if (amountInactive == 5)
- {
- Destroy(other);
- }
- int randomWaypoint = Random.Range(0, 6);
- if (waypoints[randomWaypoint].active == false)
- {
- PickRandomWaypoint(other);
- return;
- }
- if (waypoints[randomWaypoint] == other.GetComponent<ItemController>().entranceWaypoint)
- {
- PickRandomWaypoint(other);
- return;
- }
- other.GetComponent<ItemController>().exitWaypoint = waypoints[randomWaypoint];
- other.GetComponent<ItemController>().beforeCenter = false;
- }
- public void UpdatePipes(int direction)
- {
- pipeModels[direction - 1].SetActive(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement