It would make sense that I would need a collider on the gameobject, but for whatever reason it seems to be that I do not need one. The code detects everything within range just fine. My problem is that I tried doing the same exact thing in another project (unless I somehow forgot something), and I get no detection at all. I added a collider to make sure everything was right, and it worked fine, but I am confused as to why I need one here, but not in my other situation.
I plan on having hundreds of enemies on the map, and unless there is a way to optimize colliders, I cannot see a reason to have hundreds of colliders, because they just drop my FPS. The solution I switched to (Originally had colliders) worked fine in one project, but I do not understand why it will not work in this new project.
You’re not explaining yourself well beyond just providing anecdotal statements about not needing it before but you do now or along those lines.
You detect collisions with colliders or with physics queries or both in combination. That’s it. If you’re not using colliders but are using physics queries then you don’t even need a Rigidbody2D.
Your title is referring to “LayerMask” collision detection but you don’t mention it in your post. Maybe you’re not configuring the Layer Collision Matrix in your new project? No idea.
It’s just not clear what you’re saying/asking.
Doing anything drops your FPS. Maybe you mean to a point where it’s a problem. Certainly the physics query you show above isn’t the most efficient because you’re using the wasteful one that creates a full C# array. This then just gets thrown at garbage collection. You should try not to use the “All” or “NonAlloc” ones. Use the call without any suffixes and provide a reusable list i.e. Physics2D.OverlapCircle (see overloads).
I’m 100% that all of the Physics2D.OverlapCircle calls finds overlaps with Collider2D’s only! If your object only has a Rigidbody2D, it won’t be found. You probably have a collider somewhere on the instantiated object.
@MelvMay , the docs for Physics2D.OverlapCircle that you linked has some problems;
The overload with Collider2D[ ] results says nothing about what happens when there are more overlaps than there’s space in the array. I believe what happens is that the remaining results are just discarded, and that there are no guarantees about which colliders are included, but that info should probably be there!
The description “Checks if a Collider is within a circular area.” is a bit wonky for the two last overloads. I think something like “Searches for colliders within a circular area” is clearer.
Right, so possibly I may have just forgotten when I did testing prior to optimization, but it did not detect enemies when I ran the project just a few hours ago. I do remember running 3 of my classes though, and everything worked fine with them, after removing colliders, but maybe I just misremember what happened? Anyways, I did just created a new solution to this, so now I can have melee attacks.
In the enemy prefab, I have the layer set to a layer called enemy, and I use that to detect what gameobjects are within radius with that layer. I guess from what I have learned though is that I do indeed a collider.
Yes, I do mean FPS drops to the point where it is a problem. I originally was having around <10 FPS when only having 150 enemies on the map, but after removing all colliders and creating a script to handle all enemy movement, rather than each enemy having its own update function, I was up to 5000 enemies with 60+ FPS.
Simply having lots of colliders doesn’t really mean much, it’s what you’re doing. Things like modifying the Transform, which you should be doing or causing crazy numbers of contacts to be solved etc. Simply having colliders doesn’t reduce FPS. It’s not like physics iterates them all each update. I would suggest using the profiler which will tell you exactly what was taking time. It might’ve been some super slow script you were using etc.