Search Unity

Profiler Player.Loop dragging on CPU

Discussion in 'Physics' started by jleven22, Jan 14, 2022.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    I'm positive it's a physics issue. I suspect I have too many rigidbodies in my scene, but I don't know how to operate in any other way! Consider me a n00b, despite having worked on this project for years.

    I'm getting whackadoodle spikes and need some help diagnosing. It's all happening in Player.Loop and it's not pointing to any specific script. I went in and cleaned up the physics2D layer collision matrix, and this helped substantially. That's why I'm pointing at Rigidbody2D as the problem.

    Here's a screen shot of the profiler physics2D section:


    To explain my game quickly, it's Zelda NES as a twin stick shooter. So I've got a large overworld map crawling with baddies, lots of items, and large 2D tilemaps (using composite collider 2D).

    Here's a look at the overworld:
    https://imgur.com/AaIYh7G

    As you can see, not too complex. So I'm guessing it has something to do with how I've got things set up or scripted. Just not sure what it could be!

    In the profiler I'm getting hundreds of contacts. I'm looking for a workaround so that I can keep my precious RBs but not pull so hard on CPU. One thought is to turn off collider/ RB unless the player is in range of an enemy, but that really takes away from the "life" of the game. Maybe I'm on the right track with this thought process? Or should I figure out a way to reduce the amount of contacts? I'm not sure how to do this either.

    TLDR: I have a lot of rigidbody 2Ds and collider 2Ds and need help reducing CPU load.

    Thanks!
     
    Last edited: Jan 14, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    We've already spoken about this though. Hundreds of bodies isn't causing spikes. Your original problem was that you had a collider that overlapped thousand of shapes and whilst they ended-up not producing contacts, it still had to see if they should be contacts so changing your layer-collision-matrix so they could never produce a contact solved that.

    From our previous conversation, these "spikes" (4ms) are from "Physics2D.SynchronizeFixtures" so a lot of fixtures are moving so shapes/bodies.

    Total guess might be a huge tilemap that's moving.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    I'll add that if you want to update your project in the repo we set-up together and give me some simple instructions showing how to reproduce it, I'll try to take a look ASAP.

    Please note: try to strip down what's happening in the scene though. Only have active/running what is essential to reproduce the problem because otherwise I'll have to try to figure out all those scripts and what they're doing.
     
  4. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    The issue here is that I don't know how to reproduce the issue. I'm playing my game. Having major spikes on CPU usage. And I don't know what to isolate to recreate it. Deleting enemies, tilemap colliders, music areas, even the player... still getting huge drag on synchronizefixtures. I don't know how to parse down a scene to share because I don't know what is causing the issue. The profiler only takes me so far. Seems like going through my scene and turning off various objects doesn't really do anything. So tough not to feel stuck with this issue.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    The thing is, it won't "spike" for no reason. It's doing extra work because you asked it to do something. This is the isolation I was talking about.

    If these "spikes" are happening every few seconds or so then it should be easy to narrow it down. If they're happening every 5 minutes then agreed, not so easy.

    Then again, are these "spikes" actually causing a problem too?

    Nobody here on these forums is going to be able to help you better than I can. I just cannot debug it or help you with a description and I cannot spend hours figuring out all the scripts in your project and what they're doing.

    There has to be a compromise and it be narrowed down somehow. You must be able to remove as much dynamic things as possible until you can no longer see it happening.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    I'll add that in our private conversation I did describe what that profiler entry does i.e. it's this part of the physics engine: https://github.com/erincatto/box2d/...de47a0a7d25afb/src/dynamics/b2_world.cpp#L561 where the shapes attached to a body are synchronised with the engine.

    As I said above, it has to be caused by a lot of shapes (in colliders) being updated all of a sudden if it's a spike. As suggested looking at tilemaps above (they obviously have a lot of shapes); you didn't respond to that suggestion.

    I am trying to help, even willing to look through your project again but I need as simple a reproduction case as possible is all. You are not on your own with the issue, I need help understanding what is going on in it. QA would ask the same of you too. :)
     
  7. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    So I did some more digging... As it turns out, I think it's my tilemap objects using composite colliders. When I delete them from the scene, the player.loop/ synchronizefixtures is reduced significantly. Otherwise it's basically at a constant 4ms in physics, with occasional spikes.

    Perhaps it's something in my setup of the tilemap here? It's odd to me though, initially I followed the tutorials by Unity on how to set up a composite collider on a tilemap. Is there something about composite collider that could be a culprit? I'm tentative to start flipping around settings without knowing what I'm flipping or why.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Again, you're asking a question and I've tried several times to tell you I cannot debug it or tell you based on some text you type. In the end, there is no "setup" to use a composite; you just add it and click on the colliders you want to use it. It doesn't cause spikes because it's a composite.

    A collider doesn't do anything on its own. When you change it, it does stuff.

    You have to understand, you're talking about a low-level, basic thing in the physics engine (sync fixtures). The physics engine has no idea whatsoever about any Unity stuff so no composites, colliders, rigidbodies.

    I've twice mentioned about stuff moving; you've not responded to that. You have physics shapes (things that colliders create) and they are being recreated or are moving. This is the only thing that causes the SyncFixtures. Again, it's a low-level thing in the 2D physics engine (Box2D).
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Your description has changed. Previous you said you had 4ms spikes. Now you're taking 4ms synchronzing fixtures continuously? What are the spikes then? That's a huge amount of time and I don't see why you cannot narrow that down.

    This is what you said privately:

    "So it is running a lot better, but maybe I spoke too soon. I keep getting spikes around 4ms for something called Physics2D.SynchronizeFixtures"

    Has it changed then?

    Happy to take this offline in our previous private conversation if you think it'd help.

    Just get a project together, upload it to the workspace I created for you and we can go from there. Upload some instructions for the project/reproduction and what you're seeing that's bad.