public void WalkAlongPath() { Vector3 targetPosition = (Vector3)_targetNode.GetNodePosition(); if (Vector3.Distance(_position, targetPosition) < _nodeCollisionRange) { if (_pathList.IndexOf(_targetNode) != _pathList.Count - 1) { _targetNode = _pathList [_pathList.IndexOf(_targetNode) + 1]; } else { _meshObject.transform.position = targetPosition; } } else { Vector3 nodePosition = _targetNode.GetNodePosition(); _meshObject.transform.position += (nodePosition - _meshObject.transform.position).normalized * _speed * Time.deltaTime; } }
public Script_Node CalculatePath(Script_Node p_initialNode) { //Based on pseudoCode found here: https://www.raywenderlich.com/3016-introduction-to-a-pathfinding AddOpenListElementAtPosition(p_initialNode.GetNodePosition().x, p_initialNode.GetNodePosition().z, p_initialNode); while (_openDictionary.Values.Count > 0) { Script_Node currentNode = GetLowestFScoreOpenDictionary(); AddClosedListElementAtPosition(currentNode.GetNodePosition().x, currentNode.GetNodePosition().z, currentNode); RemoveOpenListElementAtPosition(currentNode.GetNodePosition().x, currentNode.GetNodePosition().z); foreach (Script_Node node in _closedDictionary.Values) { if (node.GetNodePosition().x == node.GetNodeDestination().x&& node.GetNodePosition().z == node.GetNodeDestination().z) { return(node); } } CalculateNeighbours(currentNode); } return(null); }
private void CalculateNeighbours(Script_Node p_node) { int xCurrent = p_node.GetNodePosition().x; int zCurrent = p_node.GetNodePosition().z; int xMin = xCurrent; int xMax = xCurrent; int zMin = zCurrent; int zMax = zCurrent; for (int x = xCurrent - 1; x <= xCurrent; x++) { if (x < 0) { continue; } xMin = x; break; } for (int x = xCurrent + 1; x >= xCurrent; x--) { if (x >= _grid.GetWidth()) { continue; } xMax = x; break; } for (int z = zCurrent - 1; z <= zCurrent; z++) { if (z < 0) { continue; } zMin = z; break; } for (int z = zCurrent + 1; z >= zCurrent; z--) { if (z >= _grid.GetHeight()) { continue; } zMax = z; break; } for (int z = zMin; z <= zMax; z++) { for (int x = xMin; x <= xMax; x++) { if (_grid.AccessGridTile(x, z).GetWalkable() == true && !_closedDictionary.ContainsKey(z * _grid.GetWidth() + x)) { if ((x != xCurrent && z != zCurrent)) { if (_grid.AccessGridTile(x, z + (zCurrent - z)) != null && _grid.AccessGridTile(x, z + (zCurrent - z)).GetWalkable() == false) { continue; } if (_grid.AccessGridTile(x + (xCurrent - x), z) != null && _grid.AccessGridTile(x + (xCurrent - x), z).GetWalkable() == false) { continue; } } int tempGScore = 0; if (x == xCurrent || z == zCurrent) { tempGScore = Constants.linearMovementCost; } else { tempGScore = Constants.diagonalMovementCost; } if (_openDictionary.ContainsKey(z * _grid.GetWidth() + x)) { Script_Node node = GetOpenListElement(x, z); if (node.GetParent() != null) { tempGScore += p_node.GetGScore(); } if (tempGScore < node.GetGScore()) { node.SetGScore(tempGScore); node.CalculateFScore(); node.SetParent(p_node); } } if (!_openDictionary.ContainsKey(z * _grid.GetWidth() + x)) { Vector3Int location = new Vector3Int(x, 0, z); Script_Node newNode = new Script_Node(location, p_node.GetNodeDestination(), p_node); tempGScore += newNode.GetParent().GetGScore(); newNode.SetGScore(tempGScore); newNode.CalculateHScore(); newNode.CalculateFScore(); AddOpenListElementAtPosition(x, z, newNode); } } } } }