/// <summary> /// Starts up the planner. /// </summary> /// <returns>True on success.</returns> public bool Enter() { if (agent.navGroup.query == null) { return(false); } NavmeshPoint pos = agent.GetPointSearch(agent.data.position); if (pos.polyRef == 0) { Debug.LogError(string.Format( "{0}: Could not constrain position to navigation mesh. {1}" , agent.transform.name, pos.ToString())); return(false); } NavmeshPoint goal = agent.GetPointSearch(agent.data.goal); if (goal.polyRef == 0) { Debug.LogError(string.Format("{0}: Could not constrain goal to navigation mesh. {1}" , agent.transform.name, goal.ToString())); return(false); } agent.RemoveFromCrowd(); agent.SetCorridorAssets(true); agent.SetPathAssets(false); if (agent.PlanCorridor(pos, goal) <= 0) { Debug.LogError(string.Format("{0}: Could not plan corridor on enter." , agent.transform.name)); agent.data.flags &= ~NavFlag.CorridorInUse; return(false); } agent.data.desiredPosition = agent.corridor.Position; agent.data.plannerGoal = agent.corridor.Target; agent.data.flags &= ~(NavFlag.HasNewPosition | NavFlag.HasNewGoal); mSpeed = Mathf.Sqrt(agent.data.desiredSpeedSq); mOptimizationTimer = OptimizationFrequency; return(true); }
/// <summary> /// Starts up the planner. /// </summary> /// <returns>True if successful.</returns> public bool Enter() { if (agent == null || agent.navGroup.query == null || agent.navGroup.crowd == null) { // Agent in invalid state. return(false); } NavmeshPoint pos = agent.GetPointSearch(agent.data.position); if (pos.polyRef == 0) { Debug.LogError(string.Format( "{0}: Could not constrain position to navigation mesh. {1}" , agent.transform.name, pos.ToString())); return(false); } NavmeshPoint goal = agent.GetPointSearch(agent.data.goal); if (goal.polyRef == 0) { Debug.LogError(string.Format("{0}: Could not constrain goal to navigation mesh. {1}" , agent.transform.name, goal.ToString())); return(false); } if (agent.AddToCrowd(pos.point) == null) { Debug.LogError(string.Format("{0}: Could not add agent to the crowd." , agent.transform.name)); return(false); } agent.data.desiredPosition = pos; agent.data.plannerGoal = goal; agent.data.flags &= ~(NavFlag.HasNewPosition | NavFlag.HasNewGoal); agent.SetCorridorAssets(false); agent.SetPathAssets(true); return(HandlePositionFeedback()); }
/// <summary> /// Starts up the planner. /// </summary> /// <returns>True if successful.</returns> public bool Enter() { if (agent == null || agent.navGroup.query == null || agent.navGroup.crowd == null) { return(false); } NavmeshPoint pos = agent.GetPointSearch(agent.data.position); if (pos.polyRef == 0) { Debug.LogError(string.Format( "{0}: Could not constrain position to navigation mesh. {1}" , agent.transform.name, pos.ToString())); return(false); } agent.RemoveFromCrowd(); CrowdAgentParams config = agent.crowdConfig; config.maxSpeed = 0; agent.crowdAgent = agent.navGroup.crowd.AddAgent(agent.data.plannerGoal.point, config); if (agent.crowdAgent == null) { Debug.LogError(string.Format("{0}: Could not add agent to the crowd." , agent.transform.name)); return(false); } agent.data.flags &= ~NavFlag.CrowdConfigUpdated; agent.data.desiredPosition = agent.data.position; agent.data.desiredSpeedSq = 0; agent.data.desiredVelocity = Vector3.zero; agent.SetCorridorAssets(false); agent.SetPathAssets(false); return(true); }
/// <summary> /// Initialize the planner. /// </summary> /// <remarks> /// <para> /// This method will fail on any problem, including failure to constrain the input points /// to the navigation mesh and problems adding the agent to the crowd manager. /// </para> /// </remarks> /// <returns>True if the startup was a success.</returns> public bool Enter() { if (agent.navGroup.query == null) { return(false); } NavmeshPoint pos = agent.GetPointSearch(agent.data.position); NavmeshPoint goal = agent.GetPointSearch(agent.data.goal); if (pos.polyRef == 0 || goal.polyRef == 0) { Debug.LogError(string.Format( "{0}: Could not constrain point(s) to navigation mesh. Position: {1}, Goal: {2}" , agent.transform.name, pos.ToString(), goal.ToString())); return(false); } if (agent.AddToCrowd(pos.point) == null) { Debug.LogError( string.Format("{0}: Could not add agent to crowd.", agent.transform.name)); return(false); } agent.SetCorridorAssets(false); agent.SetPathAssets(false); if (!agent.crowdAgent.RequestMoveTarget(goal)) { Debug.LogError( string.Format("{0}: Request agent move failed.", agent.transform.name)); return(false); } agent.data.plannerGoal = goal; agent.data.desiredPosition = pos; // Just in case it's new. agent.data.flags &= ~(NavFlag.HasNewPosition | NavFlag.HasNewGoal); 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()); }
/// <summary> /// Update the planner. /// </summary> /// <returns>True if the update completed successfully.</returns> public bool Update() { agent.SyncCrowdToDesired(); // Constrain the goal to the navigation mesh. NavmeshPoint ngoal = agent.GetPointSearch(agent.data.goal); if (ngoal.polyRef == 0) { Debug.LogWarning(string.Format( "{0}: Failed constrain goal to navmesh: Ignoring: {1}" , agent.transform.name, ngoal.ToString())); // Leave the flag alone. It will be handled later. ngoal = agent.data.plannerGoal; // Revert to last good. } agent.data.plannerGoal = ngoal; // Handle a full planner reset if the agent position has changed. if ((agent.data.flags & NavFlag.HasNewPosition) != 0) { NavmeshPoint pt = agent.GetPointSearch(agent.data.position); if (pt.polyRef == 0) { // Ignore the new position. Debug.LogWarning(string.Format( "{0}: Failed constrain pos to navmesh: Ignoring: {1}" , agent.transform.name, agent.data.position.ToString())); agent.data.flags &= ~NavFlag.HasNewPosition; } else { // Assume large change in position. So force a full reset of planning. // Note: Ignoring failure of add since it can only occur from invalid crowd // manager use cases. agent.AddToCrowd(pt.point); agent.crowdAgent.RequestMoveTarget(ngoal); agent.data.flags &= ~(NavFlag.HasNewPosition | NavFlag.HasNewGoal); agent.data.desiredPosition = pt; //Debug.Log(string.Format( // "{0}: Reset planning. New position." // , agent.transform.name)); return(true); } } // Feed the goal to the crowd manager. if ((agent.data.flags & NavFlag.HasNewGoal) != 0) { agent.crowdAgent.RequestMoveTarget(ngoal); agent.data.flags &= ~NavFlag.HasNewGoal; //Debug.Log(string.Format("{0}: Reset crowd goal." // , agent.transform.name)); } else { agent.crowdAgent.AdjustMoveTarget(ngoal); } // Handle posting configation changes to the crowd. if ((agent.data.flags & NavFlag.CrowdConfigUpdated) != 0) { agent.crowdAgent.SetConfig(agent.crowdConfig); agent.data.flags &= ~NavFlag.CrowdConfigUpdated; } return(true); }