- Home /
 
Detect a collision but allow prefabs to pass through each other
Hey all! How can I detect if two prefabs are colliding/intersecting while allowing them to pass through each other if the two prefabs both require Rigidbodies and circle colliders? Thanks!
Answer by thornekey · May 06, 2014 at 02:51 AM
Check the "IsTrigger" button in the inspector on your collider component.
Your tag suggests it's a 2D game.. so:
   void OnTriggerEnter2D(Collider2D other) {
         if (other.gameObject.tag == "YourTag") {
            Debug.Log("Its a hit!");
         }
   }
 
               but!, for 3D:
       void OnTriggerEnter(Collider other) {
             if (other.gameObject.tag == "YourTag") {
                Debug.Log("Its a hit!");
             }
       }
 
               and if you want to check whether is has left the trigger change "Enter" to "Exit"!
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerExit2D.html
Good Luck!
Answer by Jeff-Kesselman · May 06, 2014 at 02:05 AM
Set "IsTrigger" on the colliders.
Then have a script with OnTriggerEnter2D/OnTriggerExit2D
Your answer