Physics2D.FindNewContacts is taking 222 ms...

I’m basically running a bunch of different actors on the screen at once, which I don’t think should really be a huge problem (I’ve seen videos of Unity handling 10,000 colliders at once)

So what might be going on? Are there any tips or tricks you have to fix the issue?

1 Like

There’s simply no way for anyone to debug this with a few images so unfortunately you’re on your own here. Obviously it’s spending a lot of time finding new contacts. If you’re doing things at scale, you need ensure you’re not just stabbing away at the physics system and let it just run which includes not changing the Transform, not disabling/enabling colliders, not modifying colliders, ensure you’ve only got the layers contacting the layers that are essential, make sure if it moves that its a Rigidbody2D that’s moving (don’t modify a Static Collider) etc.

The profiler tells you more than just time; look at the Physics 2D area as it’ll also tell you contact counts.

This is a meaningless statement TBH. Colliders in themselves don’t do anything. Calculating and resolving contacts takes time, moving thousands of bodies takes time etc.

I see.

I think maybe the issue is that during frames I’m destroying and creating new physicalized projectiles. Even if I were to switch to an object pooling, I’d still be hard moving colliders around, right?

It’s a vague question that’s impossible to answer. Also, not trying to be pedantic here but colliders don’t move, Rigidbody2D do; colliders are attached to them. I say this because it’s a common mistake. Unity let’s you shoot yourself in the foot because we’re forced to “make it work” but it doesn’t mean it’s how it’s supposed to work or what’s best performance wise.

I see.

So in general, I think I might have an idea of what’s causing the issue then.

I have three main types of things in the game : actors, bullets and walls.

Each actor has a circlecollider2d on them, and a rigid body set to dynamic which is moved around using forces. They each have a child object which also has a circle collider, but slightly bigger in dimension. These are used for detecting distraction targets (other actors) via filtered overlapping, and for selecting as an attack target (clicking on the mouse). Both child objects do not have rigid bodies attached, and have istrigger = true.

Each bullet has a circlecollider2d on them, and a rigidbobdy set to dynamic which is moved around using forces. They have no child objects.

Each wall has a boxcollider2d on them with istrigger = false, but no rigidbody. They each have a child object which also have a box collider, but slightly bigger in dimension. These are used for pathfinding / raycasting, and for selecting as an attack target. Both child objects do not have rigid bodies attached, and have istrigger = true.

I have anywhere from about 50-150 walls on screen at once, and anywhere from 24-100 actors at once, and maybe up to 240 bullets at once.

When you talk about rigid bodies like that, it makes me think that maybe other objects should have rigid bodies as well? Should I add them to the walls, for instance?

On important point is that I dont think the bullets are causing the issues, since I get the lag problem as a function of the number of actors which spawn in, and not as a function of them shooting - although of course it does get worse when they shoot.

You seem to be looking at times of functions in the CPU area but NOT looking at the dedicated 2D physics area which shows you contact, body, collider counts etc. Seeing a function call takes a long time won’t tell you much if you don’t understand it but seeing that you suddenly get 10,000 contacts because you spawn things ontop of each other momentarily (as an example) does highlight something.

222ms is an externity and probably involves dealing with lots of contacts. For instance, 50 things on top of each other produces 50^2 (2500) contacts. This is all guesswork which is why we provide the profiler so you don’t have to speculate or just guess.

Yes, the frame rate slowly stacks up along with the number of contacts. I’m sitting at about 4600 contacts in this case but they steadily grow on each other as the various humans are spawned in.

Attached you’ll find a few data points : https://imgur.com/a/cGP67sE

In the last image, you see the three circle colliders. The smallest one is the “istrigger = false” one, the middle one is the selection helper that is only looked when you right click and looks for an overlapcollider on the cursor object, and the largest one gets called every 1/4 second looking for enemies it might want to shoot.

You need to isolate why you have 4600 contacts then for a relative simple scene. PolygonCollider2D can sometimes be a culprit if you’ve configured them so they produces LOTS of shapes. They can potentially cause hundreds of contacts on their own.

I certainly cannot tell you this unfortunately though so I’m not going to be much help here.

Is it possible that the parent objects have a collider2d on them, but because the child “selection helper” and “distraction_collider” objects also have colliders on them, that they doubly interact?

I tried turning this off in the Edit > Project Settings, then select the Physics2D menu… but it didn’t seem to do much of anything at all.

You’re asking me questions about your own project so I have no way of answering. Unless you’re indirectly asking me something else.

Again, it’s just not clear what you’re referring to as you describe it as “turning this off”. Turning what off?