/// <summary> /// Finds a path from the start point to the end point and loads it into the corridor. /// </summary> /// <remarks> /// <para> /// <b>Warning</b>: The corridor must be available before calling this method. /// </para> /// <para> /// The input points are all expected to be valid. (E.g. polyRef != 0) /// </para> /// </remarks> /// <param name="start"> /// A valid navigation mesh point representing the start of the path. /// </param> /// <param name="end"> /// A valid navigation mesh point representing the end of the path. /// </param> /// <returns>The length of the path, or -1 on failure.</returns> public int PlanCorridor(NavmeshPoint start, NavmeshPoint end) { // Note: Don't try to re-use the current corridor. Unlike // paths, the corridor is more likely to become malformed // over time. int pathCount; if (start.polyRef == 0 || end.polyRef == 0 || corridor == null) { return(-1); } if (start.polyRef == end.polyRef) { corridor.Reset(start); corridor.MoveTarget(end.point); data.navStatus = NavStatus.Sucess; return(1); } else { data.navStatus = navGroup.query.FindPath(start , end , navGroup.filter , agentGroup.pathBuffer , out pathCount); } corridor.Reset(start); if (pathCount > 0) { corridor.SetCorridor(end.point , agentGroup.pathBuffer , pathCount); } return(pathCount); }
//寻找路线:查找从起点到终点的路径,并将其加载到道路中 public int PlanCorridor(NavmeshPoint start, NavmeshPoint end) { //原作标识:不要尝试重用corridor,这个东西有更多的可能性变得很畸形 int pathCount; //如果点不可用就会直接失败 if (start.polyRef == 0 || end.polyRef == 0 || corridor == null) { return(-1); } //如果相同点直接结束,navStatus更新 if (start.polyRef == end.polyRef) { corridor.Reset(start); corridor.MoveTarget(end.point); navStatus = NavStatus.Sucess; return(1); } else { //调用NavmeshQuery的方法进行寻路,然后内部走NavmeshQueryEx类调用dll //最后初步猜测调用Detour的DetourNavMeshQuery(有待考证) navStatus = navGroup.query.FindPath(start, end, navGroup.filter, agentGroup.pathBuffer, out pathCount); } corridor.Reset(start); if (pathCount > 0) { corridor.SetCorridor(end.point, agentGroup.pathBuffer, pathCount); } return(pathCount); }
private void HandlePathBufferResize() { mCorridor = new PathCorridor( mPath.MaxElementCount, mCornerPolys.MaxElementCount, mGroup.query, mGroup.filter); mCorridorData = new PathCorridorData(mPath.MaxElementCount); if (mPathEnd.polyRef != 0) { FindPath(); } else if (mPathStart.polyRef != 0) { mCorridor.Reset(mPathStart); } }
private void HandleHit(RaycastHit hit) { if (Input.GetKeyDown(StdButtons.SelectA)) { if (mPathStart.polyRef == 0 || mPathEnd.polyRef == 0) { NavmeshPoint point = GetNavmeshPoint(hit.point); if (point.polyRef == 0) { return; } if (mPathStart.polyRef == 0) { mPathStart = point; mCorridor.Reset(mPathStart); return; } else { mPathEnd = point; FindPath(); return; } } else if (mPathCount > 0) { mCorridor.MovePosition(hit.point); return; } } else if (mPathCount > 0) { if (Input.GetKeyDown(StdButtons.SelectB)) { mCorridor.MoveTarget(hit.point); return; } else if (Input.GetKeyDown(StdButtons.SetA)) { NavmeshPoint point = GetNavmeshPoint(hit.point); if (point.polyRef != 0) { mPathEnd = point; FindPath(); } return; } else if (Input.GetKeyDown(StdButtons.SetB)) { NavmeshPoint point = GetNavmeshPoint(hit.point); if (point.polyRef != 0) { mPathStart = point; FindPath(); } return; } else if (Input.GetKeyDown(StdButtons.SetC)) { NavmeshPoint point = GetNavmeshPoint(hit.point); if (point.polyRef != 0) { mCorridor.OptimizePathVisibility(point.point, optimizationRange, true); mOptimizeStart = mCorridor.Position.point; mOptimizeEnd = point.point; mOptimizeTimer = TimerLength; } } } }