Advertisement
Guest User

navsphere

a guest
Jul 28th, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NavGrid : MonoBehaviour {
  5.  
  6. public Dot[,,] navGridArray;
  7. public float navsphereRadius;
  8. public float dotRadius;
  9. float dotDiameter;
  10. int gridSizeY;
  11. int halfGridSizeY;
  12.  
  13. void Start()
  14. {
  15. dotDiameter = dotRadius * 2;
  16. gridSizeY = Mathf.RoundToInt(navsphereRadius / dotDiameter);
  17. halfGridSizeY = Mathf.RoundToInt(gridSizeY / 2);
  18. CreateNavGridArray();
  19. CreateLayers();
  20. }
  21.  
  22. void CreateNavGridArray()
  23. {
  24. float maxLayerRadius = navsphereRadius;
  25. int maxCircleCount = Mathf.RoundToInt(maxLayerRadius * 2 / dotDiameter/2);
  26. float maxCircleRadius = maxCircleCount * dotDiameter;
  27. float maxCircleLength = Mathf.PI * maxCircleRadius * 2;
  28. int maxDotsCount = Mathf.RoundToInt(maxCircleLength / dotDiameter);
  29.  
  30. navGridArray = new Dot[2*halfGridSizeY+1, maxCircleCount+1, maxDotsCount+1];
  31. //Debug.Log(gridSizeY +" "+ maxCircleCount +" "+ maxDotsCount);
  32. }
  33.  
  34. void CreateLayers()
  35. {
  36. for (int y = 0 - halfGridSizeY; y< halfGridSizeY+1; y++)
  37. {
  38. Vector3 layerCenter = new Vector3(transform.position.x, transform.position.y + y*dotDiameter + dotRadius, transform.position.z);
  39. float layerRadius = Mathf.Sqrt(Mathf.Pow(navsphereRadius,2) - Mathf.Pow(y, 2));
  40. int circleCount = Mathf.RoundToInt(layerRadius*2 / dotDiameter/2);
  41. int layerNumber = y + gridSizeY / 2;
  42. CreateCircles(layerCenter, circleCount, layerNumber);
  43. //Debug.Log("___Layer done");
  44. }
  45. }
  46.  
  47. void CreateCircles(Vector3 layerCenter, int circleCount, int layerNumber)
  48. {
  49. for (int x = 0; x<circleCount; x++)
  50. {
  51. Vector3 circleCenter = layerCenter;
  52. float circleRadius = x * dotDiameter;
  53. float circleLength = Mathf.PI * circleRadius * 2;
  54. int dotsCount = Mathf.RoundToInt( circleLength / dotDiameter);
  55. CreateDots(dotsCount, circleRadius, circleLength, circleCenter, x, layerNumber);
  56. //Debug.Log("__Circle done");
  57. }
  58. }
  59.  
  60. void CreateDots(int dotsCount, float circleRadius , float circleLength, Vector3 circleCenter, int circleNumber, int layerNumber)
  61. {
  62.  
  63. for(int i = 0; i< dotsCount; i++)
  64. {
  65. Vector3 currentDot = new Vector3(circleRadius, 0, 0);
  66. Vector3 dotWorldPositionV3 = new Vector3(0, circleCenter.y, 0) + currentDot;
  67. bool isWalkable = !(Physics.CheckSphere(dotWorldPositionV3, dotRadius));
  68. navGridArray[layerNumber, circleNumber, i] = new Dot(isWalkable, dotWorldPositionV3);
  69.  
  70. //Debug.Log(layerNumber +", "+ circleNumber + ", "+ i);
  71. //Debug.Log("_Dot done");
  72.  
  73. float cosA = Mathf.Pow(dotDiameter, 2) / (2 * dotDiameter * circleRadius);
  74. float xshift = dotDiameter * cosA;
  75. float zshift = Mathf.Sqrt(Mathf.Pow(dotDiameter, 2) - Mathf.Pow(xshift, 2));
  76. currentDot = currentDot + new Vector3(xshift, 0, zshift);
  77. }
  78. }
  79.  
  80. void OnDrawGizmos()
  81. {
  82. Gizmos.DrawWireSphere(transform.position, navsphereRadius);
  83. Debug.Log(navGridArray != null);
  84. if (navGridArray != null)
  85. {
  86. foreach (Dot n in navGridArray)
  87. {
  88. if (n != null)
  89. {
  90. Gizmos.color = Color.red;
  91. Gizmos.DrawCube(n.dotWorldPositionV3, Vector3.one * (dotDiameter - 0.1f));
  92. Debug.Log(n.dotWorldPositionV3);
  93. }
  94. }
  95. }
  96. }
  97.  
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement