コード例 #1
0
ファイル: Unit.cs プロジェクト: i-sobolev/BlackSheeps
        public void MoveTo(NavMeshGridNode node)
        {
            BuildPath(node);

            if (_currentPath.IsFound && _currentPath.ResultPathNodes.Count > 0)
            {
                StartMoving();
            }
        }
コード例 #2
0
        private NavMeshGridNode FindClosestNode()
        {
            float           minDistance = Mathf.Infinity;
            NavMeshGridNode closestNode = null;

            foreach (var node in _navMeshGrid.Nodes)
            {
                var currentDistance = Vector2.Distance(transform.position, node.Position);

                if (currentDistance < minDistance)
                {
                    minDistance = currentDistance;
                    closestNode = node;
                }
            }

            return(closestNode);
        }
コード例 #3
0
        public void Update()
        {
            var horizontalInput = Input.GetAxisRaw("Horizontal");
            var verticalInput   = Input.GetAxisRaw("Vertical");

            if (horizontalInput != 0 || verticalInput != 0)
            {
                var positionDelta = new Vector2(horizontalInput, verticalInput).normalized * 0.08f;
                transform.position += (Vector3)positionDelta;

                var lastNodeIndex = _closestNode.Index;
                _closestNode = FindClosestNode();

                if (lastNodeIndex != _closestNode.Index)
                {
                    _testUnit.MoveTo(_closestNode);
                }
            }
        }
コード例 #4
0
 private void OnEnable()
 {
     _closestNode = FindClosestNode();
 }
コード例 #5
0
 public PathNode(NavMeshGridNode node, int cost)
 {
     Node = node;
     Cost = cost;
 }