123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- 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<GameObject> list_Lines;
- void Start()
- {
- player = transform.GetComponent<NavMeshAgent>();
- oldPos = player.transform.position;
- navPath = new NavMeshPath();
- targetPos = Vector3.zero;
- list_Lines = new List<GameObject>();
- }
- // 连接服务器
- // 发送自己基本信息
- // 接受服务器的信息
- // 根据传输的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<LineRenderer>();
- 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<GameObject>();
- }
- }
|