private void OnDrawGizmosSelected() { if (currentLevel?.grid == null || !Application.isPlaying) { return; } Grid grid = currentLevel.grid; for (int y = 0; y < grid.size; y++) { for (int x = 0; x < grid.size; x++) { float value = grid.nodes[x, y].preferDriveAroundPenalty / (float)Node.maxMovePenalty; if (value < 0) { value = 1f; } value = 1f - value; float red = grid.nodes[x, y].type == NodeType.Impassable ? 1f : value; Gizmos.color = new Color(red, value, value, 0.8f); Gizmos.DrawCube(grid.GridToWorld(x, y), Vector3.one * (0.9f / Grid.resolution)); } } }
private Path BuildPathFromNodes(Node start, Node end) { // Retrace var path = new List <Node>(); var current = end; while (current != start) { path.Add(current); current = current.parent; } // Simplify var wayPoints = new List <Vector3>(); var directions = new List <Vector2>(); var oldDirection = Vector2.zero; for (int i = 1; i < path.Count; i++) { var newDirection = new Vector2(path[i - 1].gridPosition.x - path[i].gridPosition.x, path[i - 1].gridPosition.y - path[i].gridPosition.y); if (newDirection != oldDirection) { wayPoints.Add(grid.GridToWorld(path[i - 1].gridPosition)); directions.Add(oldDirection); } oldDirection = newDirection; } // Build wayPoints.Reverse(); directions.Reverse(); return(new Path { points = wayPoints.ToArray(), directions = directions.ToArray() }); }