예제 #1
0
        private void ComputeAStarForSteering()
        {
            Node npcNode    = NavigationGraph.GetClosestNode(NpcManager.transform.position);
            Node playerNode = NavigationGraph.GetClosestNode(_playerData.LastSeenPosition);

            List <Node> path = Pathfinding.Astar(npcNode, playerNode);

            if (path == null || path.Count < 1)
            {
                Debug.Log(npcNode);
                return;
            }

            _currentTargetLocation = path[NpcGlobalVariables.NextElementInPath].position;
        }
예제 #2
0
파일: AiPlayer.cs 프로젝트: anterart/Mazer
    protected override void Move()
    {
        if (!gm.isTutorial)
        {
            anim.SetBool("walking", true);
        }
        Vector3 startPosition = transform.position;

        grid.StartPosition = transform;
        Vector3     targetPosition = GetTargetPosition();
        List <Node> path           = Pathfinding.Astar(startPosition, targetPosition, grid);

        grid.FinalPath = path;
        if (path != null && path.Count > 0)
        {
            Node    NextNode         = path[0];
            Vector3 moveDirection    = NextNode.Position - transform.position;
            float   moveDirectionSum = Mathf.Abs(moveDirection.x) + Mathf.Abs(moveDirection.z);
            float   moveX            = moveDirection.x / moveDirectionSum;
            float   moveZ            = moveDirection.z / moveDirectionSum;

            Vector3 movement = new Vector3(moveX, 0, moveZ);
            rb.velocity = movement * moveSpeed * Time.deltaTime;
            Vector3 lookAt = new Vector3(NextNode.Position.x, transform.position.y, NextNode.Position.z);
            transform.LookAt(lookAt);
        }
        else
        {
            Vector3 moveDirection    = targetPosition - transform.position;
            float   moveDirectionSum = Mathf.Abs(moveDirection.x) + Mathf.Abs(moveDirection.z);
            float   moveX            = moveDirection.x / moveDirectionSum;
            float   moveZ            = moveDirection.z / moveDirectionSum;

            Vector3 movement = new Vector3(moveX, 0, moveZ);
            rb.velocity = movement * moveSpeed * Time.deltaTime;
            Vector3 lookAt = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
            transform.LookAt(lookAt);
        }
    }
예제 #3
0
        private void ComputeAStarForFlock()
        {
            Node npcNode    = NavigationGraph.GetClosestNode(GetPositionOfClosestAgent());
            Node playerNode = NavigationGraph.GetClosestNode(_playerData.LastSeenPosition);

            List <Node> path = Pathfinding.Astar(npcNode, playerNode);

            pathTest = path;

            if (path == null)
            {
                Debug.Log($"The A* path is null. Last seen player position is {_playerData.LastSeenPosition}");
            }

            if (path.Count < 2)
            {
                _currentTargetLocation = _playerData.LastSeenPosition;
            }
            else
            {
                _currentTargetLocation = path[NpcGlobalVariables.NextElementInPath].position;
            }
        }
예제 #4
0
        private void FindNewTarget()
        {
            Vector3 npcPosition = NpcManager.transform.position;
            Vector3 npcVelocity = NpcManager.Rigidbody.velocity;

            Vector3 circlePosition = npcPosition +
                                     Vector3.Normalize(npcVelocity) * NpcGlobalVariables.WanderCircleDistance;

            Vector3 target = circlePosition + Random.insideUnitSphere * NpcGlobalVariables.WanderCircleRadius;

            target.y = 0;

            Node currentNode = NavigationGraph.GetClosestNode(npcPosition);
            Node targetNode  = NavigationGraph.GetClosestNode(target);

            try
            {
                _pathToTarget = new Queue <Node>(Pathfinding.Astar(currentNode, targetNode));
            }
            catch (Exception)
            {
                Debug.Log(NpcManager.name + " tried to go on a separated graph. Will stay Idle.");
                NpcManager.GetComponent <NPCStateMachine>().StartIdle();
            }

            if (_pathToTarget == null || _pathToTarget.Count < 1)
            {
                NpcManager.GetComponent <NPCStateMachine>().StartIdle();
                return;
            }

            _currentTargetNode   = _pathToTarget.Dequeue().position;
            _currentTargetNode.y = 0;

            _reachedTarget = false;
        }