maxhacker11

TriggerZone.cs

Nov 28th, 2023
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3.  
  4. public class TriggerZone : MonoBehaviour
  5. {
  6.     public bool oneShot = false;
  7.     private bool alreadyEntered = false;
  8.     private bool alreadyExited = false;
  9.  
  10.     public string collisionTag;
  11.     public UnityEvent onTriggerEnter;
  12.     public UnityEvent onTriggerExit;
  13.  
  14.     private void OnTriggerEnter2D(Collider2D collision)
  15.     {
  16.         if (alreadyEntered)
  17.             return;
  18.  
  19.         if (!string.IsNullOrEmpty(collisionTag) && !collision.CompareTag(collisionTag))
  20.             return;
  21.  
  22.         onTriggerEnter?.Invoke();
  23.  
  24.         if (oneShot)
  25.             alreadyEntered = true;
  26.     }
  27.  
  28.     private void OnTriggerExit2D(Collider2D collision)
  29.     {
  30.         if (alreadyExited)
  31.             return;
  32.  
  33.         if (!string.IsNullOrEmpty(collisionTag) && !collision.CompareTag(collisionTag))
  34.             return;
  35.  
  36.         onTriggerExit?.Invoke();
  37.  
  38.         if (oneShot)
  39.             alreadyExited = true;
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment