Пример #1
0
        private void GenerateCrowd()
        {
            if (!hasGenerated)
            {
                return;
            }

            Random rand = new Random();

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

            Vector3 c = new Vector3(0, 0, 0);
            Vector3 e = new Vector3(500f, 500f, 500f);

            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(startPt, 1000, out newPt);

                c = newPt.Position;

                //Save this random point as the starting position
                trails[i].Trail    = new Vector3[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(newPt, 1000, out targetPt);

                crowd.GetAgent(idx).RequestMoveTarget(targetPt.Polygon, targetPt.Position);
                trails[i].Trail[AGENT_MAX_TRAIL - 1] = targetPt.Position;
            }
        }
Пример #2
0
 void Update()
 {
     crowd.Update(0.02f);
     foreach (var e in entities)
     {
         e.Value.Update();
         if (e.Value is Enemy)
         {
             var enemy = e.Value as Enemy;
             UpdateEnemyPosition(e.Key, crowd.GetAgent(enemy.agentID).Position);
         }
     }
 }
Пример #3
0
    public void Init(LunaNavmeshQuery navmesh, Transform transform, System.Action idle, System.Action move)
    {
        _navQuery = navmesh;

        _transform = transform;
        _reached   = idle;
        _move      = move;

        if (_navQuery != null)
        {
            _navMeshQuery = _navQuery._navMeshQuery;
            filter        = _navQuery.filter;
            crowd         = _navQuery.Crowd;

            param = new CrowdAgentParams
            {
                Radius                = Radius,
                Height                = Height,
                MaxAcceleration       = MaxAcceleration,
                MaxSpeed              = MaxSpeed,
                CollisionQueryRange   = CollisionQueryRange,
                PathOptimizationRange = PathOptimizationRange,
                UpdateFlags           = UpdateFlags,
                ObstacleAvoidanceType = ObstacleAvoidanceType,
                SeparationWeight      = SeparationWeight
            };

            Vector3 pos = _transform.position;
            AgentId = _navQuery.Crowd.AddAgent(new[] { pos.x, pos.y, pos.z }, param);
            //ResetTarget();

            _crowdAgent = crowd.GetAgent(AgentId);
            float[] resetpos = _crowdAgent.npos;
            _transform.position = new Vector3(resetpos[0], resetpos[1], resetpos[2]);
        }
        else
        {
            UnityEngine.Debug.LogError("Scene does not have a Nav Mesh Query, one must be added.");
        }
    }
Пример #4
0
    public void Init(RoleTransform transform, LunaNavmeshQuery navmeshQuery)
    {
        _navQuery  = navmeshQuery;
        _transform = transform;


        if (_navQuery != null)
        {
            _navMeshQuery = _navQuery._navMeshQuery;
            filter        = _navQuery.filter;
            crowd         = _navQuery.Crowd;

            param = new CrowdAgentParams
            {
                Radius                = Radius,
                Height                = Height,
                MaxAcceleration       = MaxAcceleration,
                MaxSpeed              = MaxSpeed,
                CollisionQueryRange   = CollisionQueryRange,
                PathOptimizationRange = PathOptimizationRange,
                UpdateFlags           = UpdateFlags,
                ObstacleAvoidanceType = ObstacleAvoidanceType,
                SeparationWeight      = SeparationWeight
            };

            Luna3D.Vector3 pos = _transform.GetPosition();
            AgentId = _navQuery.Crowd.AddAgent(new[] { pos.x, pos.y, pos.z }, param);
            //ResetTarget();

            _crowdAgent = crowd.GetAgent(AgentId);
            float[] resetpos = _crowdAgent.npos;
            _transform.SetPosition(new Vector3(resetpos[0], resetpos[1], resetpos[2]));
        }
        else
        {
            System.Console.WriteLine("Scene does not have a Nav Mesh Query, one must be added.");
        }
    }
Пример #5
0
    // Update is called once per frame
    void Update()
    {
        if (transform.position != TargetPosition)
        {
            transform.position = Vector3.Lerp(transform.position, TargetPosition, Time.deltaTime * 50.0f);
        }
        else
        {
            if (CurrentNode < smoothPath.Count - 1)
            {
                ++CurrentNode;
                TargetPosition = ExportNavMeshToObj.ToUnityVector(smoothPath[CurrentNode]);
            }
        }

        if (crowd != null)
        {
            //Agent[] agents = new Agent[crowd.GetAgentCount()];
            for (int i = 0; i < crowd.GetAgentCount(); ++i)
            {
                lastPosition[i] = crowd.GetAgent(i).Position;
                //agents[i] = crowd.GetAgent(i);
            }

            //crowd.UpdateTopologyOptimization(agents, crowd.GetAgentCount(), Time.deltaTime);
            crowd.Update(Time.deltaTime);

            for (int i = 0; i < crowd.GetAgentCount(); ++i)
            {
                try
                {
                    //Debug.DrawLine(ExportNavMeshToObj.ToUnityVector(lastPosition[i]), ExportNavMeshToObj.ToUnityVector(crowd.GetAgent(i).Position), Color.green, 1);
                    mob[i].transform.position = ExportNavMeshToObj.ToUnityVector(crowd.GetAgent(i).Position);
                }
                catch
                {
                }
            }
        }

        if (Application.platform == RuntimePlatform.WindowsEditor)
        {   // 현재 플랫폼이 Window 에디터인지
            if (Input.GetMouseButtonDown(0))
            {
                Vector3 p = Input.mousePosition;

                Ray cast = Camera.main.ScreenPointToRay(Input.mousePosition);


                // Mouse의 포지션을 Ray cast 로 변환



                UnityEngine.RaycastHit hit;
                if (Physics.Raycast(cast, out hit))
                {
                    Debug.Log($"hit x {hit.point.x}, y {hit.point.y}, z {hit.point.z}");
                    var newPt = navMeshQuery.FindNearestPoly(ExportNavMeshToObj.ToSharpVector(hit.point), new SVector3 {
                        X = 10, Y = 10, Z = 10
                    });

                    for (int i = 0; i < crowd.GetAgentCount(); ++i)
                    {
                        NavPoint targetPt;
                        navMeshQuery.FindRandomPointAroundCircle(ref newPt, 3, out targetPt);

                        Debug.Log($"agent{i} : x {targetPt.Position.X}, y {targetPt.Position.Y}, z {targetPt.Position.Z}");

                        crowd.GetAgent(i).RequestMoveTarget(targetPt.Polygon, targetPt.Position);
                    }
                    crowd.UpdateMoveRequest();
                } // RayCast
            }     // Mouse Click
        }
    }