Esempio n. 1
0
    private Vector3 GetPathDirection(TestingSteering playerSteering, Vector3 endPos, Graph graph)
    {
        //Should have a to Vector3 instead of using gridgenerator

        //Should maybe remember index of path, and only search again if end position changes
        //If not path is found, return old direction

        //Check if there is a direct path
        //Smooth path

        //Break pathfinding. Start from new, or continue on current path
        if (_currentEndPos != endPos)
        {
            if (graph != null)
            {
                _currentEndPos    = endPos;
                _currentPath      = HierarchicalAStar.FindPath(graph, this.CurrentPosition, _currentEndPos);
                _currentPathIndex = 0;
            }
        }
        if (_currentPath != null && _currentPath.Count > 1 && _currentPathIndex < _currentPath.Count)
        {
            nextPos   = _currentPath[_currentPathIndex];
            nextPos.y = this.CurrentPosition.y;
            if (Vector3.Distance(CurrentPosition, nextPos) < _distanceToNext)
            {
                if (_currentPathIndex < _currentPath.Count - 1)
                {
                    _currentPathIndex++;
                    nextPos = _currentPath[_currentPathIndex];
                }
                else
                {
                    nextPos = endPos;
                }
                nextPos.y = this.CurrentPosition.y;
            }
            return(GetSeekDirection(this.CurrentPosition, nextPos));
        }
        return(CurrentVelocity.normalized);
    }
Esempio n. 2
0
 public static Vector3 GetDirectionVector(TestingSteering playerSteering)
 {
     if (GridGenerator.level != null && GridGenerator._currentEnd != null)
     {
         Debug.Log("Finding path");
         List <Vector3> path = HierarchicalAStar.FindPath(GridGenerator.level, playerSteering.CurrentPosition - new Vector3(0, 1, 0), GridGenerator._currentEnd.transform.position);
         Debug.Log(path[1]);
         Vector3 nodePos = path[1];
         if (nodePos == Vector3.zero)
         {
             nodePos = path[2];
             if (nodePos == Vector3.zero)
             {
                 nodePos = path[3];
             }
         }
         Vector3 seekPosition = new Vector3(nodePos.x, playerSteering.CurrentPosition.y, nodePos.y);
         return(SeekBehaviour.GetDirectionVector(playerSteering.CurrentPosition, seekPosition));
     }
     return(Vector3.zero);
 }
Esempio n. 3
0
 /*
  * public static Vector3 GetFormationDirection(Transform transform, TestingSteering leaderSteering, Vector3 positionOffset)
  * {
  *      Vector3 leadRot = leaderSteering.transform.rotation.eulerAngles;
  *      float rot = leadRot.y * Mathf.Deg2Rad;
  *
  *      float cs = Mathf.Cos(rot);
  *      float sn = Mathf.Sin(rot);
  *
  *      float newX = ((cs * positionOffset.x) - (sn * positionOffset.x));
  *      float newZ = ((sn * positionOffset.z) + (cs * positionOffset.z));
  *
  *      Vector3 tmpPos = new Vector3(newX, 0, newZ);
  *      tmpPos += leaderSteering.CurrentPosition;
  *
  *      //Vector3 tmpPos = leaderSteering.CurrentPosition + positionOffset;
  *      return SeekBehaviour.GetDirectionVector(playerSteering.CurrentPosition, tmpPos);
  * }*/
 private static Vector3 GetPathDirection(Transform transform, Vector3 endPos, Graph graph)
 {
     if (_currentEndPos[transform] != endPos)
     {
         if (graph != null)
         {
             _currentEndPos[transform]    = endPos;
             _currentPath[transform]      = HierarchicalAStar.FindPath(graph, transform.position, _currentEndPos[transform]);
             _currentPathIndex[transform] = 0;
         }
     }
     if (_currentPath[transform] != null && _currentPath[transform].Count > 1 && _currentPathIndex[transform] < _currentPath[transform].Count)
     {
         nextPos[transform] = _currentPath[transform][_currentPathIndex[transform]];
         Vector3 tmp = nextPos[transform];
         tmp.y = transform.position.y;
         nextPos[transform] = tmp;
         if (Vector3.Distance(transform.position, nextPos[transform]) < _distanceToNext)
         {
             if (_currentPathIndex[transform] < _currentPath[transform].Count - 1)
             {
                 _currentPathIndex[transform]++;
                 nextPos[transform] = _currentPath[transform][_currentPathIndex[transform]];
             }                /*
                               * else
                               * {
                               *     nextPos[transform] = endPos;
                               * }*/
             tmp   = nextPos[transform];
             tmp.y = transform.position.y;
             nextPos[transform] = tmp;
         }
         return(GetSeekDirection(transform.position, nextPos[transform]));
     }
     return(CurrentVelocity(transform).normalized);
 }