//根据目标点来转换目标,vector3 -> NavmeshPoint private NavmeshPoint GetNavPoint(Vector3 point) { if (mNavAgent == null) { return(default(NavmeshPoint)); } return(mNavAgent.GetPointSearch(point)); }
//实际上这是一个不断开闭的过程 //循环过程中也会不断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()); }
//在Start里面进行的初始化 public bool MakeInitialize() { //NavManager.ActiveManager的获取还需要一个更好的设计 if (!enabled) { Debug.LogError(this.name + ": is not enabled "); return(false); } if (NavManager.ActiveManager == null) { Debug.LogError(this.name + ": no activeManager "); return(false); } mNavManager = NavManager.ActiveManager; //建立navAgent,这是真正用于移动的agent mNavAgent = mNavManager.CreateAgent(agentGroup, transform); mNavAgent.moveSpeed = this.moveSpeed; if (mNavAgent == null) { Debug.LogError(this.name + ": agent create failed"); enabled = false; return(false); } mNavAgent.rotation = transform.rotation; mNavAgent.position = mNavAgent.GetPointSearch(transform.position); if (mNavAgent.position.polyRef == 0) { mNavAgent = null; Debug.LogError(this.name + ": take agent into navmesh failed: " + mNavAgent.position); enabled = false; return(false); } //planner是底层计算推进(可以考虑多线程) //mover是unity位置改变,只能在主线程 planner = new NavPlanner(mNavAgent); //用于表现,也就是修改Unity中的移动的move //UnitySimpleMovePlan unityMovePlan = new UnitySimpleMovePlan(); UnityMoveWithFixdRoute unityMovePlan = new UnityMoveWithFixdRoute(); unityMovePlan.SetNavAgent(mNavAgent); unityMovePlan.yAxisFreeze = this.yAxisFreeze; mover = unityMovePlan; mManagerIndex = mNavManager.AddAgent(planner, unityMovePlan); if (mManagerIndex == -1) { Debug.LogError(this.name + ": add agent to navigation manager failed"); enabled = false; return(false); } Stop(); return(true); }