Example #1
0
        /// <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);
        }
Example #2
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);
        }
Example #4
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)
            {
                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);
        }
Example #5
0
        /// <summary>
        /// Set a new follow goal. (A goal that is expected to be dynamic.)
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method will result in replanning.  Use <see cref="AdjustFollowGoal"/> to adjust
        /// the location of an existing follow goal.
        /// </para>
        /// <para>
        /// The goal rotation is ignored while in follow mode.
        /// </para>
        /// <para>
        /// The new 'goal event' will be triggered.  The 'at goal' event will be disabled while
        /// in follow mode.
        /// </para>
        /// </remarks>
        /// <param name="goal">The new goal.</param>
        /// <param name="goalType">The range type.</param>
        /// <returns>True if sucessful.</returns>
        public bool Follow(NavmeshPoint goal, RangeType rangeType)
        {
            goal = agent.GetPointSearch(goal);

            if (goal.polyRef == 0)
            {
                return(false);
            }

            agent.data.targetMode = agent.ChooseMoveToMode();
            agent.data.goal       = goal;
            agent.data.goalType   = rangeType;

            agent.data.flags = (agent.data.flags | NavFlag.HasNewGoal | NavFlag.GoalIsDynamic)
                               & ~NavFlag.GoalRotationEnabled;

            // Forces evaluation during next upate.
            mAtGoal = false;

            PostEvent(NavEventType.NewGoal);

            return(true);
        }