Ground check

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.

But I can’t seem to get this to return true!

I tried

grounded = Physics2D.IsTouchingLayers(GetComponent<Collider2D>(), LayerMask.NameToLayer ("Ground"));

Also tried

grounded = GetComponent<Collider2D>().isTouchingLayers(LayerMask.NameToLayer ("Ground"));

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.

2 Likes

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:

…isTouchingLayers(LayerMask.GetMask(“UserLayerA”, “UserLayerB”))

You can of course call this with only 1 layer too:

…isTouchingLayers(LayerMask.GetMask(“UserLayerA”))

Although it took some time to figure it out I think this is pretty neat, for it lets you check a number of layers at once.

5 Likes

THANK YOU! I couldn’t figure what was wrong. I got confused with the method asking for an int

1 Like

Dulinn, awasome, though I dont understand the logic of the masks of the layers and why we were forced to need them in the first place

1 Like

Nice to see someone using the new ‘Physics2D.IsTouchingLayers’ and ‘Physics2D.IsTouching’.

1 Like

I’m also having this problem (in Unity 2018.2.0f2). It seems IsTouchingLayers is true for an extra physics update after contact is lost.

Using the script:

public class TestIsTouching : MonoBehaviour {

    private Rigidbody2D rigidbody;
    private Collider2D collider;
    public LayerMask layerMask;

    void Start() {
        rigidbody = GetComponent<Rigidbody2D>();
        collider = GetComponent<Collider2D>();
    }

    void Update() {
        Debug.Log("Update: " + Time.time);
    }

    void FixedUpdate() {
        Debug.Log("FixedUpdate: " + Time.fixedTime);
        Debug.Log("Touching: " + Physics2D.IsTouchingLayers(collider, layerMask));

        if (Input.GetKeyDown(KeyCode.Space)) {
            Debug.Log("*********************** Jump ***********************");
            rigidbody.AddForce(10 * Vector2.up, ForceMode2D.Impulse);
        }
    }

}

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:

FixedUpdate: 0.66
Touching: True
*********************** Jump ***********************
Update: 0.670988
FixedUpdate: 0.68
Touching: True
Update: 0.6879749
FixedUpdate: 0.7
Touching: False

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.

When you set your ground layer, did you go into the layer matrix and uncheck the things you don’t wish to interact with?