//public static Vector3 GetUpPosition(Vector3 r) //{ // NavMeshHit hitInfo; // Vector3 r1 = new Vector3(r.x, r.y + 100, r.z); // if (NavMesh.Raycast(r, r1, out hitInfo, -1)) // { // r = hitInfo.position; // } // else // { // //Debug.LogError("GetUpPosition No UpPosition "); // } // return r; //} private static Vector3 GetClosetPointBySegment1(Vector3 target) { var distance = float.MaxValue; DateTime start = DateTime.Now; Vector3 r = target; if (navMeshInfo == null) { navMeshInfo = new NavMeshInfo(); } for (int i = 0; i < navMeshInfo.segments.Count; i++) { Segment3 segment = navMeshInfo.segments[i]; Vector3 point = target; Vector3 closestPoint; float dist0 = Distance.Point3Segment3(ref point, ref segment, out closestPoint); if (dist0 < distance) { distance = dist0; r = closestPoint; } } TimeSpan time = DateTime.Now - start; LogText += (string.Format( "GetClosetPointBySegment,indices count:{0},distance:{1},closestPoint{2},time:{3}ms ", navMeshInfo.indicesCount, distance, r, time.TotalMilliseconds)); return(r); }
private static Vector3 GetClosetPointByTriangle1(Vector3 target) { //var triangleParent=CreatePoint(Vector3.zero,"TriangleParent",1,Color.black); DateTime start = DateTime.Now; Vector3 r = Vector3.zero; //if (vertices == null) { var distance = float.MaxValue; //NavMeshInfo navMeshInfo = null; if (navMeshInfo == null) { navMeshInfo = new NavMeshInfo(); } for (int i = 0; i < navMeshInfo.triangle3s.Count; i++) { Triangle3 triangle3 = navMeshInfo.triangle3s[i]; Plane3 plane = navMeshInfo.planes[i]; Vector3 point = target; Vector3 closestPoint; float dist0 = Distance.Point3Plane3(ref point, ref plane, out closestPoint); Line3 line = new Line3(target, closestPoint - target); Triangle3 triangle = triangle3; Line3Triangle3Intr info; bool find = Intersection.FindLine3Triangle3(ref line, ref triangle, out info); if (find) //只有点在三角形内才有效 { //Debug.Log(string.Format("[{0}]{1} {2} {3}", i, dist0, closestPoint, find)); if (dist0 < distance) { distance = dist0; r = closestPoint; //var tri=ShowTriangle(triangleParent, i, triangle3.V0, triangle3.V1, triangle3.V2); //var pt=CreatePoint(info.Point, i + "," + info.IntersectionType + "," + info.LineParameter + "," + info.TriBary0 + "," + info.TriBary1 + "," + info.TriBary2, 0.1f, Color.black); //pt.transform.parent = tri.transform; //var pt2 = CreatePoint(closestPoint, i+","+closestPoint+","+ dist0, 0.1f, Color.black); //pt2.transform.parent = tri.transform; } } } TimeSpan time = DateTime.Now - start; LogText += (string.Format( "GetClosetPointByTriangle,indices count:{0},distance:{1},closestPoint{2},target:{3},time:{4}ms ", navMeshInfo.indicesCount, distance, r, target, time.TotalMilliseconds)); } return(r); }
private void Instance_OnWayPoint(NavMeshInfo navMeshInfo) { if (GameObject.FindGameObjectWithTag(navMeshInfo.playertag) != null) { GameObject temp = GameObject.FindGameObjectWithTag(navMeshInfo.playertag); if (EnemyManager[navMeshInfo.enemy].GetComponent <enemy>().navtarget != temp.transform) { EnemyManager[navMeshInfo.enemy].GetComponent <enemy>().navtarget = temp.transform; EnemyManager[navMeshInfo.enemy].transform.position = new Vector3(navMeshInfo.px, navMeshInfo.py, navMeshInfo.pz); } } }
void Start() { NavMeshImport navMeshImport = new NavMeshImport(); NavMeshInfo navMeshInfo = navMeshImport.Load(Application.dataPath + "/Scripts/Obj/NavMeshData.obj"); }
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"); }
public static void RefreshNavMeshInfo() { navMeshInfo = new NavMeshInfo(); }
public void HandleMessage(string message) { Debug.Log("HandleMessage:" + message); int target = message.IndexOf(":"); string tag = message.Substring(0, target); string content = message.Substring(target + 1); switch (tag) { case "register": if (OnRegisterResult != null) { OnRegisterResult(content); } break; case "login": if (OnLoginResult != null) { OnLoginResult(content); } break; case "createPlayer": RedayInfo redayinfo = GetData2 <RedayInfo>(content); if (OnCreatePlayer != null) { Debug.Log(content); OnCreatePlayer(redayinfo); } break; case "playerstatus": PlayerInfo player = GetData2 <PlayerInfo>(content); if (OnUpdataPlayer != null) { OnUpdataPlayer(player); } break; case "reday": if (content == "go!" && OnStartResult != null) { OnStartResult(content); } break; case "Tankstatus": Debug.Log(content); TankInfo tankinfo = GetData2 <TankInfo>(content); if (OnUpdateTank != null) { OnUpdateTank(tankinfo); } break; case "createEnemy": Debug.Log("收到创建"); enemyInfo enemyInfo = GetData2 <enemyInfo>(content); if (OnCreateEnemy != null) { OnCreateEnemy(enemyInfo); } break; case "GamePass": if (content == "first") { if (OnGameCount != null) { OnGameCount(content); } NetWorkManage.Instance.SendMessage("CreateEnemy:{\"enemy\":2}"); } else if (content == "second") { if (OnGameCount != null) { OnGameCount(content); } NetWorkManage.Instance.SendMessage("CreateEnemy:{\"enemy\":3}"); } else { SceneManager.LoadScene("Scenes/win"); } break; case "EnemyDie": EnemyDie enemydie = GetData2 <EnemyDie>(content); if (OnEnemyDie != null) { OnEnemyDie(enemydie); } break; case "EnemyBoom": EnemyDie enemyboom = GetData2 <EnemyDie>(content); if (OnEnemyBoom != null) { OnEnemyBoom(enemyboom); } break; case "PlayerUpdata": PlayerUpdata playerupdata = GetData2 <PlayerUpdata>(content); if (OnPlayerUpdata != null) { OnPlayerUpdata(playerupdata); } break; case "Enemytarget": NavMeshInfo navMeshInfo = GetData2 <NavMeshInfo>(content); if (OnWayPoint != null) { OnWayPoint(navMeshInfo); } break; case "MoneyUpdata": MoneyUpdata money = GetData2 <MoneyUpdata>(content); if (OnMoneyUpdata != null) { OnMoneyUpdata(money); } break; case "GameOver": SceneManager.LoadScene("Scenes/GameOver"); break; default: break; } }