Guest User

Untitled

a guest
Aug 30th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. namespace Platinio.AI
  7. {
  8.     /// <summary>
  9.     /// Generate the path and let us know when we have compute one
  10.     /// </summary>
  11.     [RequireComponent(typeof(NavMeshAgent))]
  12.     public class PathGenerator : MonoBehaviour
  13.     {
  14.         [SerializeField] private float m_maxProcessingTime = 1.0f;
  15.        
  16.         private NavMeshAgent    m_agent         = null;
  17.         private NavMeshObstacle m_obstacle      = null;
  18.         private bool            m_processing    = false;
  19.         private Coroutine       m_coroutine     = null;
  20.         private float           m_timer         = 0.0f;
  21.  
  22.         public bool Processing { get { return m_processing; } }
  23.  
  24.  
  25.         private void Awake()
  26.         {            
  27.             m_agent = GetComponent<NavMeshAgent>();
  28.             m_obstacle = GetComponent<NavMeshObstacle>();
  29.         }
  30.  
  31.        
  32.         /// <summary>
  33.         /// Calculate a new path
  34.         /// </summary>
  35.         /// <param name="position">destination</param>
  36.         /// <param name="onComplete">called when path is complete</param>
  37.         public void CalculatePath(Vector3 position , Action onComplete)
  38.         {
  39.            
  40.             if(m_coroutine != null)
  41.                 StopCoroutine(m_coroutine);
  42.  
  43.             m_coroutine = StartCoroutine( CalculatePathRoutine(position , onComplete) );
  44.         }
  45.  
  46.         public void CancelPathGeneration()
  47.         {
  48.             if (m_coroutine != null)
  49.                 StopCoroutine(m_coroutine);
  50.  
  51.             m_processing = false;
  52.         }
  53.  
  54.         private IEnumerator CalculatePathRoutine(Vector3 position , Action onComplete)
  55.         {            
  56.             m_processing = true;
  57.             m_timer = 0.0f;
  58.            
  59.             //if the obstacle is enable disable it and give a frame so the navmesh can be update and hole can be filled
  60.             if (m_obstacle.enabled)
  61.             {
  62.                 m_obstacle.enabled = false;
  63.                 yield return new WaitForSeconds(Time.deltaTime);
  64.             }
  65.  
  66.             //if the agent was disable give him a frame to
  67.             if (!m_agent.enabled)
  68.             {
  69.                 m_agent.enabled = true;
  70.                 yield return new WaitForSeconds(Time.deltaTime);
  71.             }
  72.  
  73.  
  74.             m_agent.SetDestination(position);
  75.             yield return new WaitForSeconds(Time.deltaTime);
  76.  
  77.             while ((!m_agent.hasPath || m_agent.pathStatus != NavMeshPathStatus.PathComplete || m_agent.desiredVelocity == Vector3.zero || m_agent.pathPending) && m_timer < m_maxProcessingTime)
  78.             {
  79.                 if (!m_agent.enabled)
  80.                 {
  81.                     CancelPathGeneration();
  82.                     yield break;
  83.                 }
  84.  
  85.                 yield return new WaitForSeconds(Time.deltaTime);
  86.  
  87.                 m_timer += Time.deltaTime;
  88.             }
  89.  
  90.             //wait a extra frame
  91.             yield return new WaitForSeconds(Time.deltaTime);
  92.  
  93.             //something goes wrong generating the path generate a new one
  94.             if (m_agent.enabled && ( !m_agent.hasPath || m_agent.desiredVelocity == Vector3.zero))
  95.             {
  96.                 //clean the path
  97.                 m_agent.path = new NavMeshPath();
  98.                 CalculatePath(position , onComplete);
  99.             }
  100.  
  101.             else if(m_agent.enabled)
  102.             {
  103.                 onComplete();                
  104.             }
  105.  
  106.             m_processing = false;
  107.  
  108.  
  109.  
  110.         }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment