public static Vector3 GetClosetPointEx(Vector3 target, NavMeshAgent agent) { Debug.Log("-------------------GetClosetPointEx--------------"); if (target.y > 1) { target = target + globalOffset; } currentTarget = target; //var psList = GetSampleAndEdge(target); ClearTestPoints(); //List<NavPoint> psList = new List<NavPoint>(); NavPointList psList = GetSampleAndEdgeEx(target, agent); //var p1 = GetClosetPointByTriangle(target); //psList.Add(new NavPoint("ByTriangle", p1, Color.red, agent)); //var p2 = GetClosetPointBySegment(target); //psList.Add(new NavPoint("BySegment", p2, Color.yellow, agent)); var p1 = GetClosetPointByMeshEx(target); //psList.Add(new NavPoint("ByMesh", p1, Color.red, agent)); psList.Mesh = new NavPoint("ByMesh", p1, target, Color.red, agent); var p3 = GetDownSample(target, downCount, downStep); if (p3 != target) { //psList.Add(new NavPoint("Down", p3, Color.magenta, agent)); psList.Down = new NavPoint("Down", p3, target, Color.magenta, agent); } var r = psList.GetMinDistancePoint(target, agent); if (r == null) { Log.Error("GetClosetPointEx", "NoClosetPoint:" + target + "," + agent.transform.name + "|" + psList); return(Vector3.zero); } else { //var t4 = CreatePoint(r.Position, "Closed", 0.2f, Color.black); //t4.AddComponent<NavTestInfo>().agent = agent; return(r.Position); } }
private static NavPointList GetSampleAndEdgeEx(Vector3 target, NavMeshAgent agent) { NavPointList psList = new NavPointList(); NavMeshHit hitInfo; if (NavMesh.FindClosestEdge(target, out hitInfo, -1)) { NavPoint navP = new NavPoint("Edge", hitInfo.position, target, Color.green, agent); //psList.Add(navP); psList.Edge = navP; GetDownCast(hitInfo.position, "Edge"); } else { //Debug.LogError("GetClosetPoint No FindClosestEdge "); } if (NavMesh.SamplePosition(target, out hitInfo, 100, -1)) { var dis = Vector3.Distance(hitInfo.position, target); if (dis < MaxDistance) { NavPoint navP = new NavPoint("Sample", hitInfo.position, target, Color.blue, agent); //psList.Add(navP); psList.Sample = navP; GetDownCast(hitInfo.position, "Sample"); } } else { //Debug.LogError("GetClosetPoint No SamplePosition "); } return(psList); }
public static void GetClosetPointAsync(Vector3 target, string name, NavMeshAgent agent, Action <Vector3, GameObject> callback) { //Debug.LogError("GetClosetPointAsync:" + target); if (target.y > 1) { target = target + globalOffset; } currentTarget = target; currentName = name; LogText = ""; //var psList = GetSampleAndEdge(target); ClearTestPoints(); NavPointList psList = GetSampleAndEdgeEx(target, agent); CreatePoint(target, "target", 0.1f, Color.green);//目标点 if (navMeshInfo == null) { navMeshInfo = new NavMeshInfo(); } var p1 = Vector3.zero; var p2 = Vector3.zero; ThreadManager.Run(() => { try { //p1 = GetClosetPointByTriangle(target); //p2 = GetClosetPointBySegment(target); p1 = GetClosetPointByMesh(target); //Debug.LogError("GetClosetPointByMesh:" + p1); //if(p1.y>target.y|| psList.Sample.Position.y > target.y) { p2 = GetDownMesh(target, downCount, downStep); //Debug.LogError("GetDownMesh:" + p2); } } catch (Exception e) { //Debug.LogError("GetClosetPointAsync:" + e.ToString()); } }, () => { try { //Debug.LogError("GetSamplePosition ByMesh1:" + p1); p1 = GetSamplePosition(p1); //Debug.LogError("GetSamplePosition ByMesh2:" + p1); psList.Mesh = new NavPoint("ByMesh", p1, target, Color.red, agent); //GetDownCast(p1, "ByMesh"); if (p2 != Vector3.zero) { //Debug.LogError("GetSamplePosition Down1:" + p2); p2 = GetSamplePosition(p2); //Debug.LogError("GetSamplePosition Down2:" + p2); psList.Down = new NavPoint("Down", p2, target, Color.yellow, agent); //GetDownCast(p2, "Down"); } //var p3 = GetDownSample(target); //if (p3 != target) //{ // CreatePoint(p3, "Down", 0.1f, Color.magenta); //} //var r = GetClosedPoint(psList.ToArray(), target, agent); //var p = CreatePoint(r, "Closed", 0.1f, Color.cyan);//前面这些点中的最近的点,及结果。 //Debug.LogError("psList.GetMinDistancePoint"); var r = psList.GetMinDistancePoint(target, agent); if (IsDebug) { var closedObj = CreatePoint(r.Position, "Closed", 0.2f, Color.black); closedObj.AddComponent <NavTestInfo>().agent = agent; } if (callback != null) { callback(r.Position, null); } } catch (Exception e) { if (callback != null) { callback(target, null); } } }, "GetClosetPointByTriangleEx+GetClosetPointBySegmentEx"); }