With the update to version 5 we got a cool method on colliders called isTouchingLayers(int layerMask)
and thought this would be an easy way to setup a quick check for whether or not the player is toughing the ground by placing all “ground objects” on a “Ground Layer” Same thing as Tagging essentially but not inside of a collision event.
The player game object has a boxcollider on it as well as the “ground” object which was placed on the ground layer. I also know that the integer of 8 is being returned into the function which is finding the layer correctly.
I know they’re colliding because my player object doesn’t fall through the ground, but yet can’t get this method to return true, and unfortunately the documentation is a bit sparse.
Hello,
maybe it’s a bit late and you probably already figured it out (or gave up on it), but for all those finding this post with the exact same question (like me 15min ago)… Here’s the answer.
You need to give a LayerMask to this method, not a layer, for example:
I set up one square (with a Rigidbody2D and a BoxCollider2D) sitting on top of another square (with a static BoxColldier2D). The first square has this script attached. Here’s the relevant part of the console output:
You can see that IsTouching continues to be true for a another call to FixedUpdate after the impulse is applied.
If I add a Debug.Break() in the if statement, I can see a clear separation between the two boxes at the end of the frame, but when I step ahead a frame, Touching is still returned as true.