public void MoveTo(NavMeshGridNode node) { BuildPath(node); if (_currentPath.IsFound && _currentPath.ResultPathNodes.Count > 0) { StartMoving(); } }
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); }
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); } } }
private void OnEnable() { _closestNode = FindClosestNode(); }
public PathNode(NavMeshGridNode node, int cost) { Node = node; Cost = cost; }