Move.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. public class Move : MonoBehaviour
  6. {
  7. public Transform destination;
  8. public LineRenderer rotuLine;
  9. private NavMeshAgent player;
  10. private NavMeshPath navPath;
  11. private Vector3[] rotu;
  12. private Vector3 oldPos;
  13. private Vector3 targetPos;
  14. private bool state = true;
  15. // Start is called before the first frame update
  16. private List<GameObject> list_Lines;
  17. void Start()
  18. {
  19. player = transform.GetComponent<NavMeshAgent>();
  20. oldPos = player.transform.position;
  21. navPath = new NavMeshPath();
  22. targetPos = Vector3.zero;
  23. list_Lines = new List<GameObject>();
  24. }
  25. // 连接服务器
  26. // 发送自己基本信息
  27. // 接受服务器的信息
  28. // 根据传输的Json 进行操作
  29. // 物体不存在先创建, 物体已经有了 就更新基本信息
  30. // 接受到服务器端传输的路径终点
  31. // 自行生成路线
  32. // 将路线传回服务器端
  33. // Update is called once per frame
  34. void Update()
  35. {
  36. if(Input.GetKeyDown(KeyCode.K))
  37. {
  38. SetDestination(destination.position);
  39. #region old
  40. //player.SetDestination(destination.position);
  41. //player.speed = 0;
  42. //player.CalculatePath(destination.position, navPath);
  43. //if (navPath.corners.Length <= 2)
  44. // Debug.LogError(" 路径获取不全");
  45. //else
  46. //{
  47. // rotuLine.positionCount = navPath.corners.Length;
  48. // rotuLine.SetPositions(navPath.corners);
  49. // Debug.Log(navPath.corners.Length);
  50. // for (int i = 0; i < navPath.corners.Length; i++)
  51. // {
  52. // Debug.Log(navPath.corners[i]);
  53. // GameObject.Instantiate(destination.transform, navPath.corners[i] , destination.transform.rotation);
  54. // }
  55. //}
  56. //rotu = player.path.corners;
  57. //rotuLine.positionCount = rotu.Length;
  58. //rotuLine.SetPositions(rotu);
  59. #endregion
  60. }
  61. //if(targetPos!=Vector3.zero)
  62. //if (Vector3.Distance(targetPos, player.transform.position) > player.stoppingDistance)
  63. // DrawLine(true);
  64. //else
  65. // DrawLine(false);
  66. }
  67. public void SetDestination( Vector3 pos)
  68. {
  69. pos = new Vector3(pos.x, transform.position.y, pos.z);
  70. targetPos = pos;
  71. player.SetDestination(pos);
  72. // player.speed = 1;
  73. //player.CalculatePath(pos, navPath);
  74. //if (navPath.corners.Length < 2)
  75. // Debug.LogError(" 路径获取不全");
  76. //else
  77. //{
  78. // rotuLine.positionCount = navPath.corners.Length;
  79. // rotuLine.SetPositions(navPath.corners);
  80. // Debug.Log(navPath.corners.Length);
  81. // //for (int i = 0; i < navPath.corners.Length; i++)
  82. // //{
  83. // // Debug.Log(navPath.corners[i]);
  84. // // GameObject.Instantiate(destination.transform, navPath.corners[i], destination.transform.rotation);
  85. // //}
  86. //}
  87. DrawLine();
  88. }
  89. private void DrawLine( bool state)
  90. {
  91. if(!state)
  92. {
  93. rotuLine.positionCount = 0;
  94. targetPos = Vector3.zero;
  95. return;
  96. }
  97. player.CalculatePath(targetPos, navPath);
  98. if (navPath.corners.Length < 2)
  99. Debug.LogError(" 路径获取不全");
  100. else
  101. {
  102. rotuLine.positionCount = navPath.corners.Length;
  103. rotuLine.SetPositions(navPath.corners);
  104. Debug.Log(navPath.corners.Length);
  105. //for (int i = 0; i < navPath.corners.Length; i++)
  106. //{
  107. // Debug.Log(navPath.corners[i]);
  108. // GameObject.Instantiate(destination.transform, navPath.corners[i], destination.transform.rotation);
  109. //}
  110. }
  111. }
  112. private void DrawLine()
  113. {
  114. player.CalculatePath(targetPos, navPath);
  115. if (navPath.corners.Length < 2)
  116. Debug.LogError(" 路径获取不全");
  117. else
  118. {
  119. GameObject Line = GameObject.Instantiate(rotuLine.gameObject);
  120. LineRenderer line = Line.GetComponent<LineRenderer>();
  121. line.positionCount = navPath.corners.Length;
  122. line.SetPositions(navPath.corners);
  123. Line.SetActive(true);
  124. list_Lines.Add(Line);
  125. //rotuLine.positionCount = navPath.corners.Length;
  126. //rotuLine.SetPositions(navPath.corners);
  127. Debug.Log(navPath.corners.Length);
  128. //for (int i = 0; i < navPath.corners.Length; i++)
  129. //{
  130. // Debug.Log(navPath.corners[i]);
  131. // GameObject.Instantiate(destination.transform, navPath.corners[i], destination.transform.rotation);
  132. //}
  133. }
  134. }
  135. public void CloseAllLine()
  136. {
  137. for (int i = 0; i < list_Lines.Count; i++)
  138. {
  139. list_Lines[i].SetActive(false);
  140. }
  141. list_Lines.Clear();
  142. list_Lines = new List<GameObject>();
  143. }
  144. }