private bool HandlePlanning(bool retargetPos, bool retargetGoal) { /* * Design notes: * * Tries to do a corridor move on the position and/or target. * A replan is only triggered if the move(s) fail to reach the desired polygon(s). * * It is important to note that the replan may fail to reach the goal. But that is a * problem for the AI, not the planner. */ bool needsFullReplan = false; // Convenience vars. Only used for input. Not updated. NavmeshPoint goal = agent.data.plannerGoal; NavmeshPoint pos = agent.data.desiredPosition; PathCorridor corridor = agent.corridor; if (retargetGoal && retargetPos) { corridor.Move(pos.point, goal.point); retargetGoal = (goal.polyRef != corridor.Target.polyRef || pos.polyRef != corridor.Position.polyRef); } else if (retargetPos) { corridor.MovePosition(pos.point); needsFullReplan = (pos.polyRef != corridor.Position.polyRef); } else if (retargetGoal) { corridor.MoveTarget(goal.point); needsFullReplan = (goal.polyRef != corridor.Target.polyRef); } if (needsFullReplan) { // Debug.Log("Full Replan"); if (agent.PlanCorridor(pos, goal) <= 0) { Debug.LogError(agent.transform.name + ": Could not replan corridor."); return(false); } } else if (retargetPos || retargetGoal) { // Debug.Log("Optimize Only"); // Any chagnes are likely to be larger than normal. So force an optimization. agent.corridor.OptimizePathTopology(true); mOptimizationTimer = OptimizationFrequency; } agent.data.desiredPosition = agent.corridor.Position; agent.data.plannerGoal = agent.corridor.Target; return(true); }
//这个方法是真的移动计划的移动方法 //根据路线进行移动(注意,这个Agent目标移动数据的修改) private bool HandlePlanning(bool retargetPos, bool retargetGoal) { bool needsFullReplan = false; //获取记录下来的这些信息 NavmeshPoint goal = theAgent.plannerGoal; NavmeshPoint pos = theAgent.desiredPosition; //获取路线引用 PathCorridor corridor = theAgent.corridor; if (retargetGoal && retargetPos) { //此处调用CritterAI的PathCorridor,进而调用PathCorridorEx走recast //从当前的位置转移到希望运动到的位置,另外将目标移动到希望移动到的目标 corridor.Move(pos.point, goal.point); retargetGoal = (goal.polyRef != corridor.Target.polyRef || pos.polyRef != corridor.Position.polyRef); } else if (retargetPos) { //此处调用CritterAI的PathCorridor,进而调用PathCorridorEx走recast //从当前的位置转移到希望运动到的位置 corridor.MovePosition(pos.point); needsFullReplan = (pos.polyRef != corridor.Position.polyRef); } else if (retargetGoal) { //此处调用CritterAI的PathCorridor,进而调用PathCorridorEx走recast //将目标移动到希望移动到的目标 corridor.MoveTarget(goal.point); needsFullReplan = (goal.polyRef != corridor.Target.polyRef); } if (needsFullReplan) { //完全重新计算路径 if (theAgent.PlanCorridor(pos, goal) <= 0) { //Debug.LogError(theAgent.transform.name + ": Could not replan corridor."); SetPathInformation(pos, goal); return(false); } } else if (retargetPos || retargetGoal) { //CritterAI作者加的强制优化 theAgent.corridor.OptimizePathTopology(true); mOptimizationTimer = OptimizationFrequency; } //最后重新记录位置就可以了 theAgent.desiredPosition = theAgent.corridor.Position; theAgent.plannerGoal = theAgent.corridor.Target; return(true & SetPathInformation(theAgent.desiredPosition, theAgent.plannerGoal)); }
public PathCorridor GeneratePathCorridor(Vector3 start, Vector3 end) { if (NavUtil.Failed(GetNavMeshPoint(start, new oVector3(0.3f, 2, 0.3f), out NavmeshPoint origin)) || origin.point == new oVector3()) { return(null); } if (NavUtil.Failed(GetNavMeshPoint(end, new oVector3(0.3f, 2, 0.3f), out NavmeshPoint destination)) || destination.point == new oVector3()) { return(null); } uint[] path = new uint[250]; int pathCount; if (origin.polyRef == destination.polyRef) { pathCount = 1; path[0] = origin.polyRef; } else { NavStatus status = _query.FindPath(origin, destination, _filter, path, out pathCount); if (NavUtil.Failed(status) || pathCount == 0) { // Handle pathfinding failure. throw new Exception("path failed: " + status); } else if (destination.polyRef != path[pathCount - 1]) { //Chat.WriteLine("Unable to generate full path: " + status); //throw new Exception("Unable to generate full path: " + status); //return null; } } _pathCorridor.SetCorridor(end.ToCAIVector3(), path, pathCount); _pathCorridor.Move(start.ToCAIVector3(), end.ToCAIVector3()); return(_pathCorridor); }