With so much data available to you in the Unity Profiler, you will almost automatically skip certain sections.
But often enough, these ignored sections hide great potential for performance gains.
This is the case for physics. You know, the orange-like color you see in the Unity profiler.
I'm going to show you the single checkbox-like solution that can save you up to 2 milliseconds of CPU time per frame*.
* Conditions apply.
Is Your Game Also Like This?
A client of mine was suffering from sub-30 FPS on a game that he already released on iOS.
His players were getting quite annoyed at these low performance levels. And that's not acceptable for professional game developers.
Performance optimization is a complex topic, so naturally he reached out for help.
And he got it.
My goal was to profile his game and show him the specific action points he had to take in order to achieve his goal. And more importantly, why they mattered.
So I started the video recording and went straight to profile his game.
Here's one thing I found:
You see these repeated orange blocks?
They are about physics calculation. More specifically, they are about synchronizing the transforms of your game objects with the physics engine
In a high performing game, you expect to see no more than one of these big blocks per frame.
Physics usually have a baseline cost of 1.5ms/frame on mobile. And this costs will increase linearly with the complexity of your game physics.
But here, we are talking about a couple of milliseconds that are slowing down his game.
The point is: this can be your game as well. It is actually easy to suffer from this and not to know it.
So if you open up your profiler, what do you see?
Do you see multiple blocks with the Physics.SyncColliderTransform (or similar) marker along your frame?
Do you see multiple physics calculation blocks?
If you are in the unlucky side of the equation, I'll help you fix this in just a second.
But first, let me tell you why this happens (the reason is not obvious).
Why Your Game Repeats Physics Calculations
Let me be clear: we are not talking about reducing the cost of each physics calculation block.
That would be more about optimizing colliders, reducing physics bodies, etc..
Instead, we are talking about reducing the frequency of these calculations.
So why is this happening?
You see, when you alter the transform of any game object, you force Unity to do a few things.
One of these tasks is to "sync" Unity with PhysX, the physics engine Unity uses.
If you move a game object that has a collider on it, the physics engine has to update its state. That way, your objects react correctly according to an up-to-date world.
So you might shoot a bullet. This bullet is tracked by the physics engine and Unity updates the physics engine every time the bullet moves (every frame).
What we want to do instead is to update the physics state only on the physics tick (PhysicsFixedUpdate), e.g. every 0.33ms. And certainly we don't want to do that multiple times per frame.
And just like my client, you might not know this is happening to you because it is quite implicit.
But there's a solution to this.
Fixing Physics Calculation Repetition to Gain Performance
Okay, let's see how to fix this.
Unity offers you a project setting called Auto Sync Transforms. You can find it in your (physics) project settings.
(You can also set it by code)
Short version: if you untick the Unity Physics Auto Sync Transforms option, the issue is gone.
Instead of updating the physics engine every time you move an object, unity will do it at most once in the physics tick.
You might be wondering why this is even an option if it slows down your game.
Okay, the reason is that it can have side effects.
If you are playing with the physics engine in a very special way, you might see some objects exhibit weird physics behavior.
If that's your case, you can force the physics engine to update the physics engine transforms at your will by calling Physics.SyncTransforms.
This side effect won't happen in 90%+ of the games out there. But test your game afterwards to make sure you're not part of the remaining 10%.
Did you have the Physics Auto Sync Transforms checkbox enabled?
How does the profiler look in your game?
Let me know what your gains were.
If you'd like to get a detailed performance report for your game and work with me on your game performance, apply for my Unity Performance Consulting.
~Ruben (The Gamedev Guru)