Exemplo n.º 1
0
    /// <summary>
    /// Check if the destination can be reached
    /// </summary>
    /// <param name="_position">destination to reach</param>
    /// <returns>if the destination can be reached</returns>
    public bool CheckDestination(Vector3 _position)
    {
        if (CustomNavMeshManager.Triangles == null || CustomNavMeshManager.Triangles.Count == 0)
        {
            Debug.LogWarning("Triangles Not found. Must build the navmesh for the scene");
            return(false);
        }
        if (isMoving)
        {
            StopAllCoroutines();
        }
        pathState = CalculatingState.Calculating;
        bool _canBeReached = PathCalculator.CalculatePath(OffsetPosition, _position, currentPath, CustomNavMeshManager.Triangles);

        if (_canBeReached)
        {
            pathState = CalculatingState.Ready;
            StopAllCoroutines();
            StartCoroutine(FollowPath());
        }
        else
        {
            pathState = CalculatingState.Waiting;
        }
        return(_canBeReached);
    }
Exemplo n.º 2
0
 /// <summary>
 /// Stop the agent
 /// Stop the coroutine and reset all settings
 /// </summary>
 public void StopAgent()
 {
     StopCoroutine(FollowPath());
     currentPath.PathPoints.Clear();
     isMoving  = false;
     pathState = CalculatingState.Waiting;
     OnAgentStopped?.Invoke();
 }
Exemplo n.º 3
0
 /// <summary>
 /// Calculate a path until reaching a destination
 /// </summary>
 /// <param name="_position">destination to reach</param>
 public void SetDestination(Vector3 _position)
 {
     currentPath.ClearPath();
     pathState = CalculatingState.Calculating;
     if (TDS_NavMeshManager.Instance.CalculatePath(transform.position, _position, currentPath))
     {
         pathState = CalculatingState.Ready;
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Calculate a path until reaching a destination
 /// </summary>
 /// <param name="_position">destination to reach</param>
 public void SetDestination(Vector3 _position)
 {
     if (CustomNavMeshManager.Triangles == null || CustomNavMeshManager.Triangles.Count == 0)
     {
         Debug.LogWarning("Triangles Not found. Must build the navmesh for the scene");
         //Destroy(this);
         return;
     }
     if (isMoving)
     {
         StopAllCoroutines();
     }
     pathState = CalculatingState.Calculating;
     if (PathCalculator.CalculatePath(OffsetPosition, _position, currentPath, CustomNavMeshManager.Triangles))
     {
         pathState = CalculatingState.Ready;
         StartCoroutine(FollowPath());
     }
 }
Exemplo n.º 5
0
    /// <summary>
    /// Make the agent follows the path
    /// </summary>
    /// <param name="_speed">speed</param>
    /// <returns></returns>
    public IEnumerator FollowPath(float _speed)
    {
        isMoving = true;
        TDS_CustomNavPath _pathToFollow = CurrentPath;
        int _index = 1;

        while (Vector3.Distance(transform.position, _pathToFollow.NavigationPoints.Last().Position) > .5f)
        {
            if (Vector3.Distance(transform.position, _pathToFollow.NavigationPoints[_index].Position) <= .5f)
            {
                _index = _index + 1;
            }
            transform.position = Vector3.MoveTowards(transform.position, _pathToFollow.NavigationPoints[_index].Position, Time.deltaTime * _speed);
            yield return(new WaitForEndOfFrame());
        }
        yield return(new WaitForEndOfFrame());

        pathState = CalculatingState.Waiting;
        isMoving  = false;
        CurrentPath.ClearPath();
        OnDestinationReached?.Invoke();
        yield break;
    }