//暂停,感觉其实就是刷新一下数据 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); }
//进入到状态 public override bool Enter() { if (theAgent == null || theAgent.navGroup.query == null || theAgent.navGroup.crowd == null) { return(false); } //当前的位置映射到navmesh的位置 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); } //目标映射到navmesh的位置 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); } //NavAgent开始进入到crowd if (theAgent.AddToCrowd(pos.point) == null) { Debug.LogError(string.Format("{0}: Could not add agent to the crowd.", theAgent.transform.name)); return(false); } theAgent.desiredPosition = pos; theAgent.plannerGoal = goal; theAgent.flags &= ~(NavFlag.HasNewPosition | NavFlag.HasNewGoal); theAgent.SetCorridorAssets(false); theAgent.SetPathAssets(true); return(HandlePositionFeedback()); }