Physics2D.OverlapBox not working properly?

Hey guys,

In FixedUpdate, I have canJump = Physics2D.OverlapBox(groundChecker.position, groundCheckerRadius, whatIsGround);
However, even if the Box is not hitting anything, it gets set to true every frame.
I tested this also with an if statement and printing into console.

Layers are set correctly and the box is the size I want it to be as well. I checked with

private void OnDrawGizmosSelected() {
        Gizmos.DrawCube(groundChecker.position, groundCheckerRadius);
    }

I’m really desperate. Is this a bug or something? I can’t get a simple 2D Platformer Jump working… even if I copy the tutorial 1:1. And normally I already have some experience in Unity, also coding experience… I made some more complex things already… I don’t know what to do. Please help…

Thank you in advance for anything that might help!

Yes. In your code.

Check out the documentation of this method: https://docs.unity3d.com/ScriptReference/Physics2D.OverlapBox.html

There is no overload method which takes the layers on the third parameter. (I’m assuming your whatisGround is your layer selection and not an angle…)
On the top of this, the method is returning either a Collider2d or an int, never a bool (although the int may used as boolean but you really shouldn’t).

1 Like

What Lurk says exactly… these raycast / boxcast / casting method are problematic like this:

Use Physics.Raycast() always with named arguments because it contains many poorly-designed overloads:

Thanks for your answers, but how is this possible that it works for Blackthornprod? he did the same in the video

I don’t know who that is or what video you’re talking about.

If somebody have the same problem, try to put four parameters, something like this

canJump = Physics2D.OverlapBox(groundChecker.position, groundCheckerRadius, 0f, whatIsGround);

Watch that we put in the third parameter the “0f”, if you don’t understand or you want more information see in this question in StackOverflow where there is more data about that, “c# - Physics2D.OverlapBox always returning true - Stack Overflow

1 Like