//管理目标位置,有比较的话需要对这个目标进行变更 private bool HandleNormalPlanning(bool hasNewGoal) { if (hasNewGoal || !mGoalInRange && theAgent.IsNear(theAgent.desiredPosition.point, mTarget.point)) { //在不变更移动计划的时候,看看能不能走 if (!SetLocalTarget()) { if (theAgent.PlanPath(theAgent.desiredPosition, theAgent.plannerGoal) <= 0) { Debug.LogError(string.Format("{0}: Path replan failed. Position: {1}, Goal: {2}" , theAgent.transform.name, theAgent.desiredPosition.ToString(), theAgent.plannerGoal.ToString())); return(false); } return(SetLocalTarget()); } } else if (mGoalInRange) { //这个方法根据CritterAI原有的架构来做的,由于这个部分已经被魔改,所以此处可能需要很慎重处理 if (mAtGoalState == StateNormal) { if (Vector3Util.IsInRange(theAgent.desiredPosition.point , theAgent.plannerGoal.point, 0.12f, theAgent.heightTolerence)) { mAtGoalState = StateInRange; theAgent.RemoveFromCrowd(); CrowdAgentParams config = theAgent.crowdConfig; config.maxSpeed = 0; theAgent.crowdAgent = theAgent.navGroup.crowd.AddAgent(theAgent.plannerGoal.point, config); theAgent.flags &= ~NavFlag.CrowdConfigUpdated; } } } return(true); }
//暂停,感觉其实就是刷新一下数据 private void Suspend() { mPlanner.Exit(); mPlanner = NavState.Instance; theAgent.RemoveFromCrowd(); theAgent.SetCorridorAssets(false); theAgent.SetPathAssets(true); theAgent.desiredVelocity = Vector3.zero; theAgent.desiredSpeedSq = 0; theAgent.flags &= ~NavFlag.PlannerFailed; }
//实际上这是一个不断开闭的过程 //循环过程中也会不断Enter public override bool Enter() { if (theAgent.navGroup.query == null) { return(false); } //在导航网格上找到自己的位置 NavmeshPoint pos = theAgent.GetPointSearch(theAgent.position); if (pos.polyRef == 0) { //Debug.LogError(string.Format("{0}: Could not constrain position to navigation mesh. {1}", theAgent.transform.name, pos.ToString())); return(false); } //在导航网格上找到目标的位置 NavmeshPoint goal = theAgent.GetPointSearch(theAgent.goal); if (goal.polyRef == 0) { //Debug.LogError(string.Format("{0}: Could not constrain goal to navigation mesh. {1}", theAgent.transform.name, goal.ToString())); return(false); } theAgent.RemoveFromCrowd(); theAgent.SetCorridorAssets(true); theAgent.SetPathAssets(true); //agent自己寻路,发现没有路就报错 if (theAgent.PlanCorridor(pos, goal) <= 0) { //Debug.LogError(string.Format("{0}: Could not plan corridor on enter.", theAgent.transform.name)); theAgent.flags &= ~NavFlag.CorridorInUse; return(false); } //存储寻路之后的结果 theAgent.desiredPosition = theAgent.corridor.Position; theAgent.plannerGoal = theAgent.corridor.Target; theAgent.flags &= ~(NavFlag.HasNewPosition | NavFlag.HasNewGoal); //设定速度和重置计数器 mSpeed = Mathf.Sqrt(theAgent.desiredSpeedSq); mOptimizationTimer = OptimizationFrequency; return(true); }