Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Physics2D.OverlapBox not working properly?

Discussion in 'Scripting' started by Minzent, Oct 16, 2021.

  1. Minzent

    Minzent

    Joined:
    Mar 9, 2019
    Posts:
    20
    Hey guys,

    In FixedUpdate, I have
    Code (CSharp):
    1. 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
    Code (CSharp):
    1. private void OnDrawGizmosSelected() {
    2.         Gizmos.DrawCube(groundChecker.position, groundCheckerRadius);
    3.     }
    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!
     
  2. 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).
     
    Ninlingue_1 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,516
  4. Minzent

    Minzent

    Joined:
    Mar 9, 2019
    Posts:
    20
    Thanks for your answers, but how is this possible that it works for Blackthornprod? he did the same in the video
     
  5. I don't know who that is or what video you're talking about.
     
  6. Ninlingue_1

    Ninlingue_1

    Joined:
    Feb 16, 2022
    Posts:
    1
    If somebody have the same problem, try to put four parameters, something like this
    Code (CSharp):
    1. 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, "https://stackoverflow.com/questions/55150504/physics2d-overlapbox-always-returning-true"