SebastianLague

Grid

Dec 19th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NavGrid : MonoBehaviour {
  6.  
  7.     public Vector2 size;
  8.     public float nodeSize = 1;
  9.     public LayerMask collisionMask;
  10.     public float height = 10;
  11.     public Transform test;
  12.  
  13.     Node[,] debugNodes;
  14.  
  15.     [ContextMenu("Gen nodes")]
  16.     public Node[,] GenerateNodes()
  17.     {
  18.         Vector2Int numNodes = NumNodes;
  19.         Node[,] nodes = new Node[numNodes.x,numNodes.y];
  20.  
  21.        // Vector3 rayTopLeft = new Vector3(-nodeSize * numNodes.x / 2f, height, nodeSize * numNodes.y / 2f);
  22.  
  23.         for (int y = 0; y < numNodes.y; y++)
  24.         {
  25.             for (int x = 0; x < numNodes.x; x++)
  26.             {
  27.                 Vector3 rayPos = WorldPosCentreOfCoordinate(x, y) + Vector3.up * height;
  28.                 RaycastHit hit;
  29.  
  30.                 Vector3 nodePosition = new Vector3(rayPos.x, 0, rayPos.z);
  31.                 bool nodeIsTraversable = false;
  32.  
  33.                 if (Physics.Raycast(rayPos, Vector3.down, out hit, height * 2, collisionMask))
  34.                 {
  35.                     nodePosition = hit.point;
  36.      
  37.                     if (hit.collider.tag == "Ground")
  38.                     {
  39.                         nodeIsTraversable = true;
  40.                     }
  41.                 }
  42.                 nodes[x, y] = new Node(nodeIsTraversable, nodePosition, new Vector2Int(x,y));
  43.  
  44.             }
  45.         }
  46.         debugNodes = nodes;
  47.         return nodes;
  48.     }
  49.  
  50.     public Vector3 WorldPosCentreOfCoordinate(int x, int y)
  51.     {
  52.         Vector3 topLeft = new Vector3(-nodeSize * (NumNodes.x-1) / 2f,0, nodeSize * (NumNodes.y-1) / 2f);
  53.         return topLeft + (Vector3.right * x + Vector3.back * y) * nodeSize;
  54.     }
  55.  
  56.     public Vector2Int CoordinateFromWorldPos(Vector3 pos)
  57.     {
  58.         int x = Mathf.RoundToInt((pos.x) / nodeSize + (NumNodes.x - 1) * .5f);
  59.         int y = Mathf.RoundToInt((-pos.z) / nodeSize + (NumNodes.y - 1) * .5f);
  60.         return new Vector2Int(Mathf.Clamp(x, 0, NumNodes.x - 1), Mathf.Clamp(y, 0, NumNodes.y - 1));
  61.     }
  62.  
  63.     Vector2Int NumNodes
  64.     {
  65.         get
  66.         {
  67.             return new Vector2Int(Mathf.RoundToInt(size.x / nodeSize), Mathf.RoundToInt(size.y / nodeSize));
  68.         }
  69.     }
  70.  
  71.     private void OnDrawGizmos()
  72.     {
  73.         Vector2Int coordFromWorldTest = CoordinateFromWorldPos(test.position);
  74.         for (int y = 0; y < NumNodes.y; y++)
  75.         {
  76.             for (int x = 0; x < NumNodes.x; x++)
  77.             {
  78.                 if (x == coordFromWorldTest.x && y == coordFromWorldTest.y)
  79.                 {
  80.                     Gizmos.DrawCube(WorldPosCentreOfCoordinate(x, y) + Vector3.up * height, new Vector3(nodeSize, 0, nodeSize));
  81.                 }
  82.                 else
  83.                 {
  84.                     Gizmos.DrawWireCube(WorldPosCentreOfCoordinate(x, y) + Vector3.up * height, new Vector3(nodeSize, 0, nodeSize));
  85.                     if (debugNodes != null)
  86.                     {
  87.                         if (!debugNodes[x, y].isTraversable)
  88.                         {
  89.                             Gizmos.color = new Color(1, 0, 0, .5f);
  90.                             Gizmos.DrawCube(WorldPosCentreOfCoordinate(x, y) + Vector3.up * height, new Vector3(nodeSize, 0, nodeSize));
  91.                             Gizmos.color = Color.white;
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment