JeCodeLeSoir

Triiget navLink

Jul 16th, 2023
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using Unity.AI.Navigation;
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4.  
  5. public class AntTriggerLink : MonoBehaviour
  6. {
  7.     [SerializeField] private NavMeshAgent meshAgent;
  8.     [SerializeField] private BehaviorAnt behaviorAnt;
  9.  
  10.     private void Reset()
  11.     {
  12.         meshAgent = GetComponent<NavMeshAgent>();
  13.         behaviorAnt = GetComponent<BehaviorAnt>();
  14.     }
  15.  
  16.     bool Trigg;
  17.  
  18.     int area = -1;
  19.     AnteaterManager anteaterManager = null;
  20.    
  21.     private void LateUpdate()
  22.     {
  23.         if (meshAgent.isOnOffMeshLink)
  24.         {
  25.             var offMeshLinkData = meshAgent.currentOffMeshLinkData;
  26.  
  27.             if (offMeshLinkData.offMeshLink is not null)
  28.             {
  29.                 var offMeshLink = offMeshLinkData.offMeshLink;
  30.                 anteaterManager = offMeshLink.GetComponent<AnteaterManager>();
  31.                 area = offMeshLink.area;
  32.             }
  33.             else
  34.             {
  35.                 var MeshLink = meshAgent.navMeshOwner as NavMeshLink;
  36.                 if (MeshLink is not null)
  37.                 {
  38.                     anteaterManager = MeshLink.GetComponent<AnteaterManager>();
  39.                     area = MeshLink.area;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         if (Trigg != meshAgent.isOnOffMeshLink)
  45.         {
  46.             if (!meshAgent.isOnOffMeshLink)
  47.             {
  48.                 if (area > -1 && anteaterManager is not null)
  49.                 {
  50.                     if (area
  51.                         == NavMesh.GetAreaFromName("Enter"))
  52.                     {
  53.                         anteaterManager.Register(behaviorAnt);
  54.                         Debug.Log("Enter");
  55.                     }
  56.  
  57.                     if (area
  58.                        == NavMesh.GetAreaFromName("Exit"))
  59.                     {
  60.                         anteaterManager.UnRegister(behaviorAnt);
  61.                         Debug.Log("Exit");
  62.                     }
  63.  
  64.                     area = -1;
  65.                     anteaterManager = null;
  66.                 }
  67.             }
  68.             Trigg = meshAgent.isOnOffMeshLink;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment