Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class NavGrid : MonoBehaviour {
- public Dot[,,] navGridArray;
- public float navsphereRadius;
- public float dotRadius;
- float dotDiameter;
- int gridSizeY;
- int halfGridSizeY;
- void Start()
- {
- dotDiameter = dotRadius * 2;
- gridSizeY = Mathf.RoundToInt(navsphereRadius / dotDiameter);
- halfGridSizeY = Mathf.RoundToInt(gridSizeY / 2);
- CreateNavGridArray();
- CreateLayers();
- }
- void CreateNavGridArray()
- {
- float maxLayerRadius = navsphereRadius;
- int maxCircleCount = Mathf.RoundToInt(maxLayerRadius * 2 / dotDiameter/2);
- float maxCircleRadius = maxCircleCount * dotDiameter;
- float maxCircleLength = Mathf.PI * maxCircleRadius * 2;
- int maxDotsCount = Mathf.RoundToInt(maxCircleLength / dotDiameter);
- navGridArray = new Dot[2*halfGridSizeY+1, maxCircleCount+1, maxDotsCount+1];
- //Debug.Log(gridSizeY +" "+ maxCircleCount +" "+ maxDotsCount);
- }
- void CreateLayers()
- {
- for (int y = 0 - halfGridSizeY; y< halfGridSizeY+1; y++)
- {
- Vector3 layerCenter = new Vector3(transform.position.x, transform.position.y + y*dotDiameter + dotRadius, transform.position.z);
- float layerRadius = Mathf.Sqrt(Mathf.Pow(navsphereRadius,2) - Mathf.Pow(y, 2));
- int circleCount = Mathf.RoundToInt(layerRadius*2 / dotDiameter/2);
- int layerNumber = y + gridSizeY / 2;
- CreateCircles(layerCenter, circleCount, layerNumber);
- //Debug.Log("___Layer done");
- }
- }
- void CreateCircles(Vector3 layerCenter, int circleCount, int layerNumber)
- {
- for (int x = 0; x<circleCount; x++)
- {
- Vector3 circleCenter = layerCenter;
- float circleRadius = x * dotDiameter;
- float circleLength = Mathf.PI * circleRadius * 2;
- int dotsCount = Mathf.RoundToInt( circleLength / dotDiameter);
- CreateDots(dotsCount, circleRadius, circleLength, circleCenter, x, layerNumber);
- //Debug.Log("__Circle done");
- }
- }
- void CreateDots(int dotsCount, float circleRadius , float circleLength, Vector3 circleCenter, int circleNumber, int layerNumber)
- {
- for(int i = 0; i< dotsCount; i++)
- {
- Vector3 currentDot = new Vector3(circleRadius, 0, 0);
- Vector3 dotWorldPositionV3 = new Vector3(0, circleCenter.y, 0) + currentDot;
- bool isWalkable = !(Physics.CheckSphere(dotWorldPositionV3, dotRadius));
- navGridArray[layerNumber, circleNumber, i] = new Dot(isWalkable, dotWorldPositionV3);
- //Debug.Log(layerNumber +", "+ circleNumber + ", "+ i);
- //Debug.Log("_Dot done");
- float cosA = Mathf.Pow(dotDiameter, 2) / (2 * dotDiameter * circleRadius);
- float xshift = dotDiameter * cosA;
- float zshift = Mathf.Sqrt(Mathf.Pow(dotDiameter, 2) - Mathf.Pow(xshift, 2));
- currentDot = currentDot + new Vector3(xshift, 0, zshift);
- }
- }
- void OnDrawGizmos()
- {
- Gizmos.DrawWireSphere(transform.position, navsphereRadius);
- Debug.Log(navGridArray != null);
- if (navGridArray != null)
- {
- foreach (Dot n in navGridArray)
- {
- if (n != null)
- {
- Gizmos.color = Color.red;
- Gizmos.DrawCube(n.dotWorldPositionV3, Vector3.one * (dotDiameter - 0.1f));
- Debug.Log(n.dotWorldPositionV3);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement