Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Physics2D OverlapCircleAll not returning anything.

Discussion in 'Physics' started by rell023, May 15, 2023.

  1. rell023

    rell023

    Joined:
    Mar 29, 2023
    Posts:
    18
    Point, Radius, and Layer Mask are all set to the appropriate values. The target collider is attached to a dynamic rigidbody and set to trigger. The rigidbodies are also set to always awake, so they aren't asleep as I've seen some other posts suggest. Despite all this, the return array is empty. I thought it could be blocking colliders, so I turned off all irrelevant colliders and still nothing, though the function wouldn't be very good at collecting all colliders in a radius if colliders blocked eachother. Does anyone have any experience with this function that could point me in the right direction?

    Thanks!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,256
    The function is fine and you don't really need experience with it as it doesn't exactly what it says it does. :)

    If there are no colliders there or you're providing args that are wrong then of course it won't return anything. I know that seems obvious but it's all that it can be really.

    I'd say check what you're passing to the call by debugging it (it's easy to make assumptions) but if you want help, you'll need to show all that detail really. Common mistakes are passing the layer-mask wrong, getting arguments in the wrong order, colliders not on the layers you think etc.

    You don't need to worry about it being anything to do with any physics component setting such as a rigidbody being asleep as that will not affect physics queries (it wouldn't make sense). A body being asleep doesn't mean all attached colliders are somehow invisible so I'd say ignore what you've read on that. :) Also, I don't know what you mean by "blocking" collider. If you mean somehow obscuring the query then again that wouldn't make much sense because the call is simply asking what colliders exist in that region.

    If you have no idea then create an empty project, place a collider at the origin and perform the same query at (0,0) with a radius of 1. You'll see it returning that single collider.

    Alternately, post your script that calls that and show the collider details that is at the position you expect. Maybe you've just missed something from staring at it too long?
     
  3. rell023

    rell023

    Joined:
    Mar 29, 2023
    Posts:
    18
    Thank you for the in depth response. Not really sure what I mean by "blocking" either, I don't understand the physics engine at all and am regurgitating what I've heard other people say in the hopes I get corrected lol. Finding the terminology you need to be searching for is the most simple, yet hardest part of game dev.

    Heres the script, apologies for the visual scripting, I'm a blueprints pleb that needed to make a quick transition to unity for a school project




    In this first image is where the event is called, the point is set to the position of the object, radius to the range, which in this case is 7, and layer mask to 6.

    I originally used an oncollisionenter event combined with a circle collider to detect enemies, but that didn't work if an enemy was already inside the collision circle when this state in the state machine was entered, and oncollisionstay was not calling for some reason.

    I've seen another post in the meantime suggesting that turning on Auto Sync Transforms would fix this particular issue, however it ended up breaking all of my colliders, not sure why, I don't understand the magic behind the physics engine. I've also seen (I believe in one of your posts) that you should not drive physics objects by changing the transform, however this is attached to a nav mesh agent and that is unavoidable.




    Here is what is happening in the scene. The circle collider on the character to the bottom right is set on begin play to the same variable that is setting the radius of the overlap circle. So it should be not only detecting the little box collider on the upper left, but also the circle collider I was using for oncollisionenter. However the return array length is still 0 as you can see in the first screenshot which was taken while paused at the same time.




    In this third image you can see that the box collider is set to layer 6




    And finally, here is the components and settings for the CollisionBox object.

    Thank you for your time and patience

    * I just noticed the box collider wasn't set to trigger, perhaps I did just have blinders on from staring at it too long, lemme see if it works

    **Didn't change anything, I guess the circle collider would have been picked up if that was the issue since it was set to trigger.
     
    Last edited: May 17, 2023
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,256
    If I had to suggest something, it'd be look for basic mistakes because you've posted lots of things that are potentially going wrong but you're using a basic function so the mistake will be a basic one. This is what I was trying to get to previously; this call is so simple and works fine so you've made a basic mistake and no complex physics system thing will cause it to fail.

    So, it's LayerMask not Layer (it's a bitmask for multiple layers). Looks like you need to look up what the difference is (see video below). This obviously applies to everything in Unity that uses it, not just this call.

    LayerMask 6 does not mean Layer 6. It means layers 1 and 2.

    No idea why that VS node doesn't give you the usual LayerMask selection to select multiple layers but I don't work on VS and I've never used it.

     
    rell023 likes this.
  5. rell023

    rell023

    Joined:
    Mar 29, 2023
    Posts:
    18
    I had no clue there was a difference, thank you, I will look into this
     
  6. rell023

    rell023

    Joined:
    Mar 29, 2023
    Posts:
    18
    Thanks for the guidance @MelvMay, solved it. If anyone else comes here looking for an answer I personally found this tutorial did a very good job of explaining the theory behind Bitmasks. If anyone is having trouble figuring out how bitshifting is done in visual scripting here's your answer:
    The formula node is your friend. Ignore the red, I dropped the node during PIE and it had no inputs.