Advertisement
UnityCoder_Jay

IntersectingObjects.cs

Jul 26th, 2021 (edited)
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using UnityEngine; // A header/namespace that lets us use UnityEngine..
  2.  
  3. // A call called "IntersectingObjects" this will be different from case to case basis..
  4. // that inherits MonoBehaviour which is an API..
  5. public class IntersectingObjects : MonoBehaviour {
  6.  
  7.     // Create string with the public access modifier called "tagName"
  8.     public string tagName;
  9.  
  10.     // A method in Unity to detect when objects intersect one another.
  11.     private void OnTriggerEnter2D(Collider other)
  12.     {
  13.         // When the other object with the tag value of "tagName" collides
  14.         if (other.gameObject.CompareTag(tagName))
  15.         {
  16.             // We destroy the gameObject that interseceted with it.
  17.             Destroy(gameObject);
  18.  
  19.             // If you want to destroy the object you touch instead of yourself, for instance:
  20.             // Your character is collecting a coin you could do:
  21.             Destroy(other.gameObject);
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement