// Use this for initialization
        void Start()
        {
            character = GetComponent <CharacterController>();
            navMesh   = GameObject.FindObjectOfType <DungeonNavMesh>();
            if (navMesh == null)
            {
                Debug.LogWarning("Cannot build initialize dungeon navigation agent. No dungeon navigation object found in the scene. Drop in the DungeonNavigation prefab into the scene");
            }
            else
            {
                // Place the player on the nearest valid nav mesh polygon
                PositionOnNearestNavMesh();

                var agentParams = new SharpNav.Crowds.AgentParams();
                agentParams.Radius                = radius;
                agentParams.Height                = height;
                agentParams.MaxAcceleration       = maxAcceleration;
                agentParams.MaxSpeed              = maxSpeed;
                agentParams.CollisionQueryRange   = collisionQueryRange;
                agentParams.PathOptimizationRange = pathOptimizationRange;
                agentParams.SeparationWeight      = separationWeight;
                agentParams.UpdateFlags           = UpdateFlags.Separation | UpdateFlags.OptimizeTopo;

                var position  = transform.position;
                var sposition = ToSV3(position);
                if (navMesh.Crowd == null)
                {
                    Debug.Log("Navmesh not initialized properly.  Crowd is null");
                    return;
                }
                agentId = navMesh.Crowd.AddAgent(sposition, agentParams);

                if (agentId >= 0)
                {
                    agent = navMesh.Crowd.GetAgent(agentId);
                }
                else
                {
                    Debug.Log("Cannot create crowd nav agent");
                }
            }
        }
示例#2
0
        private void GenerateCrowd()
        {
            if (!hasGenerated || navMeshQuery == null)
                return;

            Random rand = new Random();
            crowd = new Crowd(MAX_AGENTS, 0.6f, ref tiledNavMesh);

            SVector3 c = new SVector3(10, 0, 0);
            SVector3 e = new SVector3(5, 5, 5);

            AgentParams ap = new AgentParams();
            ap.Radius = 0.6f;
            ap.Height = 2.0f;
            ap.MaxAcceleration = 8.0f;
            ap.MaxSpeed = 3.5f;
            ap.CollisionQueryRange = ap.Radius * 12.0f;
            ap.PathOptimizationRange = ap.Radius * 30.0f;
            ap.UpdateFlags = new UpdateFlags();

            //initialize starting positions for each active agent
            for (int i = 0; i < numActiveAgents; i++)
            {
                //Get the polygon that the starting point is in
                NavPoint startPt;
                navMeshQuery.FindNearestPoly(ref c, ref e, out startPt);

                //Pick a new random point that is within a certain radius of the current point
                NavPoint newPt;
                navMeshQuery.FindRandomPointAroundCircle(ref startPt, 1000, out newPt);

                c = newPt.Position;

                //Save this random point as the starting position
                trails[i].Trail = new SVector3[AGENT_MAX_TRAIL];
                trails[i].Trail[0] = newPt.Position;
                trails[i].HTrail = 0;

                //add this agent to the crowd
                int idx = crowd.AddAgent(newPt.Position, ap);

                //Give this agent a target point
                NavPoint targetPt;
                navMeshQuery.FindRandomPointAroundCircle(ref newPt, 1000, out targetPt);

                crowd.GetAgent(idx).RequestMoveTarget(targetPt.Polygon, targetPt.Position);
                trails[i].Trail[AGENT_MAX_TRAIL - 1] = targetPt.Position;
            }
        }
示例#3
0
        /// <summary>
        /// Add an agent to the crowd.
        /// </summary>
        /// <param name="pos">The agent's position</param>
        /// <param name="parameters">The settings</param>
        /// <returns>The id of the agent (-1 if there is no empty slot)</returns>
        public int AddAgent(Vector3 pos, AgentParams parameters)
        {
            //find empty slot
            int idx = -1;
            for (int i = 0; i < maxAgents; i++)
            {
                if (!agents[i].IsActive)
                {
                    idx = i;
                    break;
                }
            }

            if (idx == -1)
                return -1;

            agents[idx].UpdateAgentParameters(parameters);

            //find nearest position on the navmesh and place the agent there
            NavPoint nearest;
            navQuery.FindNearestPoly(ref pos, ref ext, out nearest);
            /*if (status == false)
            {
                nearest = pos;
                reference = 0;
            }*/

            agents[idx].Reset(nearest.Polygon, nearest.Position);
            agents[idx].IsActive = true;

            return idx;
        }