Checking for Ground with Boxcast- Platforms

Hello, there has been an issue I have been having in my game that has been plaguing me for a while. My character uses a 2D box cast to detect the ground underneath him, however if he jumps through a one-way platform there are a few frames of time where the box cast detects that he is on (or rather in) the ground and if you time it right he will be lodged in the ground. Granted, you aren’t stuck you just have to jump again for it to be right, but its still not ideal for me.

I’ve been doing some research and testing but I cannot for the life of me found the correct way for my player to not get stuck in a one-way platform. I’ve read up on Contact Filters (which I do not understand) and I’ve learned that creating stopgaps like detected the players y velocity won’t be enough for it to be 100% fool proof. If anyone would be kind enough to help me out I’d appreciate it.

public bool isGrounded() //checking ground
    {
        RaycastHit2D raycastHit = Physics2D.BoxCast(groundCheck.bounds.center, groundCheck.bounds.size, 0f, Vector2.down, groundLayers);
        Color rayColor;
        if (raycastHit.collider != null)
        {
            rayColor = Color.green;
        }
        else
        {
            rayColor = Color.red;
        }
        Debug.DrawRay(groundCheck.bounds.center + new Vector3(groundCheck.bounds.extents.x, 0), Vector2.down * (groundCheck.bounds.extents.y), rayColor);
        Debug.DrawRay(groundCheck.bounds.center - new Vector3(groundCheck.bounds.extents.x, 0), Vector2.down * (groundCheck.bounds.extents.y), rayColor);
        Debug.DrawRay(groundCheck.bounds.center - new Vector3(groundCheck.bounds.extents.x, groundCheck.bounds.extents.y), Vector2.right * (groundCheck.bounds.extents.x), rayColor);
        return raycastHit.collider != null;
    }

My ground check is a boolean that gets called througout my code, so I don’t exactly know how I can alter it to have more parameters as-is.

I would like to know where you read that, I use the y velocity and it hasn’t been a problem, I needed it to check for fall damage as well. I also use edge colliders for my one way platforms instead of a box collider, it works fine. I also don’t use box cast I use overlap box because its a cheaper operation.

I can’t find the unity forum post that said it atm (pretty sure it was dated around 2015), but it definitely won’t work for me either way. I made it so that if the player lets go of the jump button their y velocity instantly becomes 0 and they fall, which means that if I time it perfectly you will get stuck anyways.

I’ll definitely think about switching to overlap box though!

Here’s a solid method for checking ground-state that includes more control for filtering: Is there some way to get two BoxColliders sided normals at the same time? (Is Grounded checking)

1 Like