Esempio n. 1
0
        /// <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>
        /// 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);
        }