Physics2D OverlapCircleAll not returning anything.

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!

The function is fine and you don’t really need experience with it as it doesn’t exactly what it says it does. :slight_smile:

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. :slight_smile: 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?

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.

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.

1 Like

I had no clue there was a difference, thank you, I will look into this

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.

what is the logic here? why i can’t give the layer name or a simple number?

LayerMask.LayerToName(number) gives me the correct name

LayerMask.NameToLayer(string) gives me the correct layer

OverlapCircleAll(circle center, circle radius, layerMask wants some crazy bitwise math???)

why I can’t just give it ONE layer and ignore the others?

why?

all the documentation is confusing

https://docs.unity3d.com/ScriptReference/LayerMask.html

quote"The first 8 of these Layers are specified by Unity"

except the grey out layers are 5 and there is a random layer 3 that you can use9857208--1419726--meh.png

TL;DR What you need instead of LayerMask.NameToLayer is LayerMask.GetMask.

Think of a mask as an array of 32 booleans instead of a 32 bit number. So normally, 6 in binary is 110 – but that value, as a mask, actually indicates that the booleans at index 1 and 2 are true - i.e., layers 1 and 2. For layer 6 as a mask, you need to shift that bit “1” to 6 times to get 1000000. You can type 1 << 6 in code (or probably use GetMask)

So now, I’m going to complain about public static implicit operator LayerMask(int intVal) which turns an int into a LayerMask – but not by converting a Layer ID into a mask, no no, it just assumed you already have a mask stored in that int. Easy room for confusion, which this thread is about. LayerMask as a different type is supposed to prevent mixing with other types, but then it will convert an int without you asking, without actually converting an id to a mask.

Sigh. I mean, I first saw LayerMask as an Overlap argument and thought “it’s masking the collisions to this layer only”, not “a mask of multiple layers”, so I passed the layer id. Which hits the exact problem from this thread. If only it didn’t decide to convert int to layermask, I wouldn’t