No it doesn’t alloc even though it’s a (tentatively) deprecated method really. Take the “NonAlloc” suffix off any query and use that one which has all the overloads including List support. I nyour case, Physics2D.OverlapCircle.
Looks like it’s the callback that’s allocating. You can stop that by selecting Physics2D.reuseCollisionCallbacks (this is on by default though) so that the “Collision2D” instance is reused.
BTW: Your code is wrong. You’re interating all the array if you get at least a single result. You should only iterate the result count that the query returns because this is all the call populates. Also, “playerTransform.localPosition” isn’t a world-space position; it’s a position relative to the parent.
Actually I just spotted that you are creating an array yourself:
Collider2D[] result = new Collider2D[10];
This will obviously produce garbage. You should reuse that array so take it outside that function.
As a side note: You’re much better off though using the overload that allows you to pass a “List” as that will grow only if needed and if not, no garbage
mmm…thanks for the little tips extra that i did not noticed before like that array inside. sadly OverlapCircleNonAlloc does not looks like having a List Overload.
I never said it did. As I said above, don’t use the deprecated NonAlloc calls. Just use “Physics2D.OverlapCircle” as I said. There you can use an array or list.
So do you follow what I said about the source of the garbage being you creating an array inside the function? I only ask because you didn’t acknowledge it.
Yeah, it’s technically not deprecated because it would cause a lot of users to be frustrated but all 2D queries with “NonAlloc” suffix are tentatively deprecated. All 2D queries (without exception) have an overload that replaces these calls (that take an array) and also accept a list.
OverlapCircleNonAlloc → OverlapCircle
RaycastNonAlloc → Raycast
etc.
Note that there’s a much easier method of detecting if something is grounded by using “IsTouching” and “IsTouchingLayers”: Unity - Scripting API:
You can an example of this in my GitHub repo. Here’s a video with links if you’re interested.
Wow i do not know how i did not found any reference to that before. It is the bad side of learn Everything on YouTube, ALMOST every body utilize the same functions.