コード例 #1
0
        public static float CalculatePathCost(Vector3 sourcePosition, Vector3 targetPosition, int areaMask)
        {
            sourcePosition = sourcePosition.Round(_fastCalculatePathGridSize);
            targetPosition = targetPosition.Round(_fastCalculatePathGridSize);
            var key = new Tuple3 <Vector3, Vector3, int>(sourcePosition, targetPosition, areaMask);

            if (_fastCalculatePathCache.ContainsKey(key))
            {
                return(_fastCalculatePathCache[key]);
            }

            NavMeshPath path = new NavMeshPath();
            float       cost;

            if (NavMesh.CalculatePath(sourcePosition, targetPosition, areaMask, path))
            {
                if (path.status == NavMeshPathStatus.PathComplete)
                {
                    cost = path.Cost();
                }
                else
                {
                    cost = float.MaxValue;
                }
            }
            else
            {
                cost = float.MaxValue;
            }

            _fastCalculatePathCache[key] = cost;
            return(cost);
        }