using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class Move : MonoBehaviour { public Transform destination; public LineRenderer rotuLine; private NavMeshAgent player; private NavMeshPath navPath; private Vector3[] rotu; private Vector3 oldPos; private Vector3 targetPos; private bool state = true; // Start is called before the first frame update private List list_Lines; void Start() { player = transform.GetComponent(); oldPos = player.transform.position; navPath = new NavMeshPath(); targetPos = Vector3.zero; list_Lines = new List(); } // 连接服务器 // 发送自己基本信息 // 接受服务器的信息 // 根据传输的Json 进行操作 // 物体不存在先创建, 物体已经有了 就更新基本信息 // 接受到服务器端传输的路径终点 // 自行生成路线 // 将路线传回服务器端 // Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.K)) { SetDestination(destination.position); #region old //player.SetDestination(destination.position); //player.speed = 0; //player.CalculatePath(destination.position, navPath); //if (navPath.corners.Length <= 2) // Debug.LogError(" 路径获取不全"); //else //{ // rotuLine.positionCount = navPath.corners.Length; // rotuLine.SetPositions(navPath.corners); // Debug.Log(navPath.corners.Length); // for (int i = 0; i < navPath.corners.Length; i++) // { // Debug.Log(navPath.corners[i]); // GameObject.Instantiate(destination.transform, navPath.corners[i] , destination.transform.rotation); // } //} //rotu = player.path.corners; //rotuLine.positionCount = rotu.Length; //rotuLine.SetPositions(rotu); #endregion } //if(targetPos!=Vector3.zero) //if (Vector3.Distance(targetPos, player.transform.position) > player.stoppingDistance) // DrawLine(true); //else // DrawLine(false); } public void SetDestination( Vector3 pos) { pos = new Vector3(pos.x, transform.position.y, pos.z); targetPos = pos; player.SetDestination(pos); // player.speed = 1; //player.CalculatePath(pos, navPath); //if (navPath.corners.Length < 2) // Debug.LogError(" 路径获取不全"); //else //{ // rotuLine.positionCount = navPath.corners.Length; // rotuLine.SetPositions(navPath.corners); // Debug.Log(navPath.corners.Length); // //for (int i = 0; i < navPath.corners.Length; i++) // //{ // // Debug.Log(navPath.corners[i]); // // GameObject.Instantiate(destination.transform, navPath.corners[i], destination.transform.rotation); // //} //} DrawLine(); } private void DrawLine( bool state) { if(!state) { rotuLine.positionCount = 0; targetPos = Vector3.zero; return; } player.CalculatePath(targetPos, navPath); if (navPath.corners.Length < 2) Debug.LogError(" 路径获取不全"); else { rotuLine.positionCount = navPath.corners.Length; rotuLine.SetPositions(navPath.corners); Debug.Log(navPath.corners.Length); //for (int i = 0; i < navPath.corners.Length; i++) //{ // Debug.Log(navPath.corners[i]); // GameObject.Instantiate(destination.transform, navPath.corners[i], destination.transform.rotation); //} } } private void DrawLine() { player.CalculatePath(targetPos, navPath); if (navPath.corners.Length < 2) Debug.LogError(" 路径获取不全"); else { GameObject Line = GameObject.Instantiate(rotuLine.gameObject); LineRenderer line = Line.GetComponent(); line.positionCount = navPath.corners.Length; line.SetPositions(navPath.corners); Line.SetActive(true); list_Lines.Add(Line); //rotuLine.positionCount = navPath.corners.Length; //rotuLine.SetPositions(navPath.corners); Debug.Log(navPath.corners.Length); //for (int i = 0; i < navPath.corners.Length; i++) //{ // Debug.Log(navPath.corners[i]); // GameObject.Instantiate(destination.transform, navPath.corners[i], destination.transform.rotation); //} } } public void CloseAllLine() { for (int i = 0; i < list_Lines.Count; i++) { list_Lines[i].SetActive(false); } list_Lines.Clear(); list_Lines = new List(); } }