Physics.Simulate - Spikes - Taking too much time from frame to frame

I am optimizing my game and after bunch of improvements I still noticed some spikes which are noticeable by player. Both shallow and deep profiling show same spikes caused by Physics.Simulate, but none of them shows enough information of what exactly takes time for simulation. 2 frames which gave some information are shown bellow.

  • Game contains almost no physics, no collisions during spikes, no dynamic bodies.
  • No moving colliders without Rigidbody (only simple trigger colliders with kinematic rigid bodies)
  • First 2 captures are from the moment when kinematic bullets with trigger colliders are moved using rb.MovePosition() without any collision and triggering.
  • Next 2 captures are taken from timespan when no bullet is moving,
  • Last one capture no enemies, no static environment colliders are used (meaning that spikes are regular)
  • Captures are taken on iPhone 14 pro, same spikes are noticeable on Samsung A51, Xiaomi

I can provide any necessary detail that can help to solve the issue.

The only markers I could spot so far (if were not Idle on Job workers) that take half or almost all time of simulation were the following:

  • PhysX.ScScene.KinematicShapeUpdateTask
  • PhysX.ScScene.finalizeUpdateTask
  • PhysX.ScScene.rigidBodyNarrowPhase
  • PhysX.DirtyShapeUpdatesTask

Unity Editor version : 2021.3.32f1

It’s hard to really diagnose this just from the capture and I’m not a physics person so someone can probably come along and provide more info than me but to me, it looks like you’re just operating over too many RigidBodies.

Also if you look at the first spike it correlates with there being a lot of overlaps and you say that it’s bullets? In that case maybe the bullets are overlapping with each other as they move/spawn?

You could try and remove the RB component from the bullets and just have colliders on them while moving them with transform.position and also make sure when they spawn they aren’t spawning on top of each.