Collider2D collider = GetComponent(); bool isTouching = Physics2D.IsTouchingLayers(collider, LayerMask.GetMask("Ground")); if (isTouching) { // Do something when the collider is touching a collider from the ground layer }
Collider2D collider = GetComponentIn this example, we specify an array of layer names for the method to check against. We then check if the collider is touching a collider from either the "Ground" or "Obstacle" layer. The Physics2D IsTouchingLayers method is part of the UnityEngine.Physics2D package library.(); int[] layersToCheck = { LayerMask.NameToLayer("Ground"), LayerMask.NameToLayer("Obstacle") }; bool isTouching = Physics2D.IsTouchingLayers(collider, LayerMask.GetMask(layersToCheck)); if (isTouching) { // Do something when the collider is touching a collider from either the ground or obstacle layer }