public void RefreshTransitions() { for (int x = 0; x < Graph.Width; x++) { for (int y = 0; y < Graph.Height; y++) { var point = new Vector2Int(x, y); var position = GetCellPoint(point); NeighborsService.GetNeighbors(Graph, point, neighborsBuffer); foreach (var i in neighborsBuffer) { var neighbor = GetCellPoint(i); var dir = (neighbor - position).normalized; var distance = Vector3.Distance(neighbor, position); var transitionCost = WalkableCost; if (Physics.Raycast(position, dir, distance, RaycastLayers.value) || Physics.Raycast(neighbor, -dir, distance, RaycastLayers.value)) { transitionCost = NotWalkableCost; } Graph.SetTransition(point, i, transitionCost); } } } }
private void OnDrawGizmos() { if (Graph == null || NeighborsService == null) { return; } if (!Application.isPlaying) { RefreshTransitions(); } for (int x = 0; x < Graph.Width; x++) { for (int y = 0; y < Graph.Height; y++) { var point = new Vector2Int(x, y); var position = GetCellPoint(point); NeighborsService.GetNeighbors(Graph, point, neighborsBuffer); foreach (var i in neighborsBuffer) { var neighbor = GetCellPoint(i); var dir = (neighbor - position).normalized; var distance = Vector3.Distance(neighbor, position); var transition = Graph.GetTransition(point, i); Gizmos.color = Color.Lerp(Color.green, Color.red, transition); Gizmos.DrawLine(position, neighbor); } Gizmos.color = Color.cyan; Gizmos.DrawSphere(position, 0.8f); } } }