contactMask
Calculates the effective LayerMask that the Collider2D will use when determining if it can contact another Collider2D.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Property
- Namespace: UnityEngine
- Assembly: UnityEngine.Physics2DModule
public LayerMask contactMask { get; }
Remarks
The returned mask is calculated using a combination of the layer collision matrix and both the Rigidbody2D and Collider2D layer overrides.; more detail is provided in the code example below:
Additional Resources: Collider2D.CanContact.
Examples
using UnityEngine;public class Example : MonoBehaviour{ void Start() { var myCollider = GetComponent<Collider2D>(); Debug.Log(myCollider.contactMask); Debug.Log(CalculateContactMask(myCollider)); } LayerMask CalculateContactMask(Collider2D collider) { Rigidbody2D body = collider.attachedRigidbody; LayerMask layerCollisionMask = Physics2D.GetLayerCollisionMask(collider.gameObject.layer); LayerMask includeMask = collider.includeLayers | (body ? body.includeLayers : new LayerMask()); LayerMask excludeMask = collider.excludeLayers | (body ? body.excludeLayers : new LayerMask()); return (layerCollisionMask | includeMask) & ~excludeMask; }}