Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Unit : MonoBehaviour {
  5.  
  6. public Grid grid;
  7. Transform target;
  8. public Transform sawmill;
  9. public Transform tree;
  10. public float speed = 20;
  11. Vector3[] path;
  12. int targetIndex;
  13. int startIndex;
  14. float timeTaken;
  15.  
  16. private void Update() {
  17. timeTaken = Time.time;
  18. }
  19.  
  20. void Start() {
  21. target = tree;
  22. PathRequestManager.RequestPath(transform.position,target.position, OnPathFound);
  23. Debug.Log("Found the tree");
  24. /*
  25. List<Node> surrounding = grid.GetNeighbours(grid.NodeFromWorldPoint(transform.position));
  26. foreach (Node n in surrounding)
  27. {
  28. grid.Discovered(n.gridX, n.gridY)
  29. }
  30. */
  31. }
  32.  
  33. public void OnPathFound(Vector3[] newPath, bool pathSuccessful) {
  34. if (pathSuccessful) {
  35. path = newPath;
  36. targetIndex = 0;
  37. StopCoroutine("FollowPath");
  38. StartCoroutine("FollowPath");
  39. }
  40. }
  41.  
  42. IEnumerator FollowPath() {
  43. Vector3 currentWaypoint = path[0];
  44. while (true) {
  45. if (transform.position == currentWaypoint) {
  46. targetIndex ++;
  47. if (targetIndex >= path.Length) {
  48. target = sawmill;
  49. PathRequestManager.RequestPath(transform.position,target.position, OnPathFound);
  50. yield break;
  51. }
  52. currentWaypoint = path[targetIndex];
  53. }
  54. transform.position = Vector3.MoveTowards(transform.position,currentWaypoint,speed * Time.deltaTime);
  55. yield return null;
  56. }
  57. }
  58.  
  59. void OnTriggerEnter(Collider other) {
  60. if (other.gameObject.tag == "Target") {
  61. Debug.Log("choppy choppy the tree");
  62. Debug.Log("Going to the sawmill!");
  63. Destroy(other.gameObject);
  64. }
  65. if (other.gameObject.tag == "Sawmill") {
  66. Debug.Log("at the sawmill " + (Time.time - timeTaken) + "ms");
  67. }
  68. }
  69.  
  70. public void OnDrawGizmos() {
  71. if (path != null) {
  72. for (int i = targetIndex; i < path.Length; i ++) {
  73. Gizmos.color = Color.black;
  74. Gizmos.DrawCube(path[i], Vector3.one);
  75.  
  76. if (i == targetIndex) {
  77. Gizmos.DrawLine(transform.position, path[i]);
  78. }
  79. else {
  80. Gizmos.DrawLine(path[i-1],path[i]);
  81. }
  82. }
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement