Skip to content

Instantly share code, notes, and snippets.

@birdinforest
Last active July 19, 2016 01:48
Show Gist options
  • Save birdinforest/7c9468dbda79bd03c7700cd7ddac1d71 to your computer and use it in GitHub Desktop.
Save birdinforest/7c9468dbda79bd03c7700cd7ddac1d71 to your computer and use it in GitHub Desktop.
Ignore collision if collider math given layer mask or collider's tag is in given tags list. NOTE: This is perminently ignoring in whole game runtime.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Ignore collision if collider math given layer mask or collider's tag is in given tags list.
/// NOTE: This is perminent ignoring in whole game runtime.
/// </summary>
public class CollisionIgnore : MonoBehaviour {
[SerializeField] private LayerMask ignoreMask;
[SerializeField] private List<string> ignoreTags = new List<string>();
private Collider2D[] myColliders;
private void Awake() {
myColliders = GetComponents<Collider2D>();
}
private void OnCollisionEnter2D(Collision2D c) {
LayerMask mask = 1 << c.collider.gameObject.layer;
string tag = c.collider.tag;
if((mask & ignoreMask) == mask || ignoreTags.Contains(tag)) {
for (int i = 0; i < myColliders.Length; i++)
{
Physics2D.IgnoreCollision(myColliders[i], c.collider);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment