Advertisement
JoeCoo7

Untitled

Mar 5th, 2021
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. private static int MAX_TRIES = 5;
  2. private static Vector3[] m_pathCorners = new Vector3[MAX_CORNERS];
  3. private static NavMeshPath m_navMeshPath = new NavMeshPath();
  4.  
  5. //----------------------------------------------------------------------------------------
  6. public static bool GetNavMeshPosition(Vector3 _pos, int _areaMask, float _maxDistance, out Vector3 _navMeshPos)
  7. {
  8.     NavMeshHit navMeshHit;
  9.     if (NavMesh.SamplePosition(_pos, out navMeshHit, _maxDistance, _areaMask))
  10.     {
  11.         _navMeshPos = navMeshHit.position;
  12.         return true;
  13.     }
  14.     _navMeshPos = Vector3.negativeInfinity;
  15.     return false;
  16. }
  17.  
  18. //----------------------------------------------------------------------------------------
  19. public static int CalculatePath(Vector3 _start, Vector3 _target, NavMeshQueryFilter _filter, Vector3[] _path, int _startIndex = 0, int _numTries = 0)
  20. {
  21.     if (_numTries > MAX_TRIES || _startIndex >= m_pathCorners.Length)
  22.     {
  23.         if (_numTries > MAX_TRIES)
  24.             Debug.Log("could not find a way from" + _start + " to " + _target + " in " + _numTries);
  25.         else
  26.             Debug.Log("could not find a way from" + _start + " to " + _target + " as" +_startIndex + "is samller or equal than" + m_pathCorners.Length);
  27.         return -1;
  28.     }
  29.  
  30.     var startPosOk = GetNavMeshPosition(_start, _filter.areaMask, MAX_PATH_DISTANCE, out Vector3 startPos);
  31.     var endPosOk = GetNavMeshPosition(_target, _filter.areaMask, MAX_PATH_DISTANCE, out Vector3 targetPos);
  32.  
  33.     if (!startPosOk || !endPosOk)
  34.     {
  35.         Debug.Log("calculate path could not get proper navmesh position of start "+ _start  + " and/or target position " + _target);
  36.         return -1;
  37.     }
  38.  
  39.     NavMesh.CalculatePath(startPos, targetPos, _filter, m_navMeshPath);
  40.     switch (m_navMeshPath.status)
  41.     {
  42.         case NavMeshPathStatus.PathPartial:
  43.         {
  44.             var numCorners = m_navMeshPath.GetCornersNonAlloc(m_pathCorners);
  45.             Array.Copy(m_pathCorners, 0, _path, _startIndex, numCorners);
  46.             var lastPos = m_navMeshPath.corners[m_navMeshPath.corners.Length - 1];
  47.             if (Math.Abs(lastPos.x - targetPos.x) < 0.5f && Math.Abs(lastPos.z - targetPos.z) < 0.5f)
  48.                 return _startIndex + numCorners;
  49.  
  50.             return CalculatePath(lastPos, _target, _filter, _path, _startIndex + numCorners, ++_numTries);
  51.         }
  52.         case NavMeshPathStatus.PathComplete:
  53.         {
  54.             var numCorners = m_navMeshPath.GetCornersNonAlloc(m_pathCorners);
  55.             Array.Copy(m_pathCorners, 0, _path, _startIndex, numCorners);
  56.             return _startIndex + numCorners;
  57.         }
  58.         default:
  59.         {
  60. #if DEBUG                
  61.             Debug.Log("path invalid" + _start + " to " + _target + " in " + _numTries + " filter: " + _filter.areaMask);
  62. #endif                
  63.             return -1;
  64.         }
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement