Stop player standing in crawl space

Hi guys,

im using this

private bool isGrounded()
    {
        RaycastHit2D raycast2D = Physics2D.BoxCast(playerStandingCollider.bounds.center, playerStandingCollider.bounds.size, 0f, Vector2.down, .1f, Ground);
       
        return raycast2D.collider != null;
    }

to detect when my player is grounded as to perform a jump

my player can crawl, and i want to put him in a passage that he cant stand in, at the moment he can though.
My idea was to have the ground collider where he has has to crawl, layerd as MustCrawl. I couldnt work out how to get the above raycast to check for another layer, and i thought it might get messy.

So i created a similar return bool (Is that what you would call it?) called mustCrawl.
but when i print (mustCrawl) its always false., regardless if im on the MustCrawl layer or not.

am i on the right track here? any help appreciated.

Since you’ve left out everything to do with mustCrawl, I must ask if you are handling it properly:

Layers vs Masks:

Yes, you’re. But you need to check the documentation (always!):
https://docs.unity3d.com/ScriptReference/Physics2D.BoxCast.html

There is a direction parameter which tells the cast which direction you would like to look.
Now, the groundcheck and the headcheck are virtually the same, just the direction is the opposite. I will leave it to you to find out how you can check it.

I got it solved thanks. My original idea was working, i just never assigned Crawl layer in the inspector.
Thanks guys!!!

2 Likes