I’m making a game about building spaceships from various blocks. Some time ago I started doing a major update, the main features of which are changing the structure of the ship (from a lot of rigidbody2d connected using a fixedJoint to colliders with a rigidbody2d parent), as well as moving to a new version of Unity (from 2019.4 to 2021.2).
After reworking the project, I found that the effect of switching to the new version of Unity was just the opposite - periodically fps began to drop to single-digit values (in the old version, under the same conditions, there were stable 60+ fps).
With the help of the profiler, we managed to find out that lags are caused by physical simulation, in particular - FindNewContacts (95% of the time), while in the old version of Unity there were no such lags.
During the experiments, I managed to find out that the lags disappear if the Poligon Collider is turned off for some objects, but this still does not explain why the performance on the new version of Unity is lower than in the old versions.
Is it possible to fix the lags without getting rid of the objects with the Poligon Collider and without changing the Unity version?
You made significant changes to the structure of your game and updated Unity, now you’re finding performance issues so it’s Unity and nothing to do with your restructuring? If you don’t know what caused it, why would you know it’s Unity causing it? 2D Physics hasn’t caused FindNewContacts to suddenly be called more. It doesn’t so this because it wants to, it does it because your code is forcing it to evaluate new contacts. This is inside Box2D (the underlying 2D engine) which hasn’t changed in years and that part pretty much has never changed, no matter what version of Unity you’re using so this statement is difficult to understand. FindNewContacts is here in Box2D The only difference in our copy, we have a profiler entry so you see it.
So the profiler tells you more than a call cost, there’s metrics for numbers of contacts, shapes, bodies etc too. Each 2D Collider tells you how many shapes it’s producing which is important for multi-shape colliders like the PolygonCollider2D. A guess would be you’re creating crazy numbers of contacts or at least causing them to be evaluated; potentially because you have PolygonCollider2D that have a LOT of shapes and they all overlap or are not producing contacts but you are not using the Collision Layers to tell them to ignore each other.
You’ve not posted profiler detail so it’s all guesswork. The fact that you didn’t mention that the solver is taking a lot of time solving contacts but mention that it’s spending time finding new contacts would suggest my guess is true; it’s spending time figuring out if contacts should be produced by not producing them.
In help narrowing it down, try finding the worst culprits and make sure they’re set to only produce contacts with things you need them too using the Layer Collision Matrix. Make sure you’re never modifying the Transform for “movement” too and look at contact counts. If you’ve got low contact counts but high FindNewContacts it means you’re causing a lot of things to be checked for new contacts, for instance if you’re modifying Static colliders or modifying the Transform causing colliders to be recreated etc.
Testing the old version on the new version of Unity showed that the bugs were most likely caused by my new system, but I still don’t understand why.
Checking the contacts of each collider in the scene yielded no results - 0 contacts, but FindNewContacts is still thousands.
Changing the transform to move is not used.
There are no more colliders and shapes than in the old version without lags.
Also, I don’t fully understand what exactly you mean by “modifying static colliders”.
What specific profiler details do I need to post to help you solve my problem?
As I said, FindNewContacts is finding them, not actually creating them. Thousands of what?
Hopefully you know what a Static collider is and that you should not be moving it or modifying it. It’s very common to see Collider2D with no Rigidbody2D (implicitly Static) being moved. In a broader sense though, I’m referring to anything that asks the physics system to regenerate a Collider2D from scratch. If you change the radius of a CircleCollider2D, it’ll need to be recreated and contacts for it will need to be discovered (FindNewContacts). This is no different than if you disabled and reneabled a collider.
Maybe post anything. So far you’ve not posted anything. Show the profiler output. Show the CPU call for FindNewContacts, show at the same time the Physics 2D section.
In terms of debugging it, I cannot do that for you and cannot debug a description. You have to figure out what colliders are being regenerated, or modified or where you have a lot of shapes overlapping which are not excluded in the Layer Collision Matrix. You need to narrow it down really.
The culprit, from what you’ve said, are some (or a lot) of PolygonCollider2D overlapping or in contact.
Intersection or contact of a large number of Poligon Colliders does not occur - the objects do not even interact with each other.
I asked for the 2D physics metrics. You are showing Networking, Video, 3D Physics etc and the 2D physics stuff is off the screen. Did you notice?
I mean, you’ve got 40 bodies with how many shapes (you don’t show) but are producing 9 thousand contacts and by the sound of it, if you’re hitting FindNewContacts each update then you’re doing what I said above.
It’s easy to say that but the above is clearly showing that you are doing that, if not with a PolygonCollider2D then something else. Physics doesn’t just call that for fun.
Turn everything off in the Layer Collision Matrix. What does it show you then?
Stats at the same time as FindNewContacts.
I don’t think I can add more info to what I’ve said though.
Collision matrix set up, profiler with the right (hopefully) information…
By the way, can the decrease in FPS be related to the structure of the ship used - parts with colliders and without a rigidbody, with a parent object that has a rigidbody?
Yep, in terms of numbers of bodies and shapes, no problem. Contacts are pretty high but the problem you’re having is finding new contacts which is caused by what I said previously.
Again, you say this but that doesn’t mean it’s true.
That starts happening at some point in your project when something changes. I cannot know what that is but you should know. By not saying it as well as above you’re kind of indicating that nothing is changing, nothing is happening to cause this. Things are not moving or overlapping etc. That can’t be true.
What does that mean?
Should I ask this again?
A significant performance drop occurs shortly after the ship starts moving near the asteroid field (motionless objects with PolygonCollider) and the station (similar ship). In another part of the same scene with the same objects, there is no FPS drop. I can record a video if that helps in any way. Nowhere among the scripts present on the scene, there is a modification in colliders, which I double-checked several times.
Possibly a translation problem. The profiler is specified here in the situation where the collision matrix is configured in the same way as usual.
Objects pass through each other as expected, FPS remains high.
So start selecting specific layers back in until you see the performance tank. At least that way you’ll know which group of colliders are responsible for it.
Videos won’t help. I’m sure it’s slow when taking 2 seconds finding new contacts.
I cannot promise when I can get a chance to look but if you are desperate and cannot figure this out then DM me with your email and I can create a private workspace for you to upload the project or preferably a cut-down, minimal version of it with as few moving parts as possible along with instructions on how to duplicate the issue.
Thanks! But how to DM here? I’m still not completely comfortable with how to use this site.
The Inbox at the top. You can start a conversation by clicking on my profile name at the top then on that page, start a conversation (DM).
BTW: Can I ask what version of Unity you are using? The only reason I ask is I’m hoping it’s not related to the above issue (although I doubt it): Collision matrix not working
2021.2.1f1
The collision matrix functions normally, so it’s probably not related.
Yes, it’s fixed there.