Physics.SyncTransforms

Hello,

I noticed that the plugin calls Physics.SyncTransforms twice in a FixedUpdate call. This is causing a lot of CPU overhead, is there a reason for it?

A specific graph needing it, for example? I use the Recast graph, will it need this type of functionality? It seems there is no comment why it is necessary, and I would love to bring down CPU usage in any way I can.

1 Like

Hi

SyncTransforms will primarily do work that would have been done anyway (but perhaps later). They are used to make sure that the raycasts that the movement scripts perform hit up to date colliders, if some other script has moved them. Similarly, it is called at the end again to make sure other scripts know about the updated positions of the agents.

Hi @aron_granberg, I mentioned this earlier in this thread.

The Unity dev that works on the Physics engine explicitly called out this practice (of calling SyncTransforms) as not being necessary in this Unity Forums post. From his comment in that thread:

You should never ever have to call this. The reason is because you should never change the Transform to change the pose of physics components. The only thing that moves in physics is the Rigidbody(2D). It’s that which writes to the Transform at the end of the simulation step. If you’re not simulating per-frame then this is why the Rigidbody(2D) provides interpolation.

If I’m not mistaken, AIBase is moving pathfinding agents using the attached Rigidbody/Rigidbody2D, correct? If so then you should easily be able to get rid of the calls to SyncTransforms in your code base. I’ve already tested out this theory; I was able to comment out the calls to Physics.SyncTransforms and Physics2D.SyncTransforms in Core/AI/AIBase.cs without any change to how AIPath moves my pathfinding agents on the graph (in my case GridGraphs).

Can we please get rid of the calls to SyncTransforms from AIBase? At the very least, could you please add a value that allows end users to toggle syncing of Transforms on/off during AIBase/OnUpdate? Thanks.

Hi

I agree that in a perfect world, it wouldn’t be required. But sadly, people really do not do things properly a lot of the time. If I don’t include the SyncTransform calls, I will inevitably get people who are confused about why things are not working (something I did get a lot before I added those calls).

Is it really that expensive? I’ve never seen it show up as a significant part when I’ve been profiling.

Would it be possible to add a toggle that enables/disables SyncTransforms during AIBase/OnUpdate (defaults to enabled)?

Last time I did profiling of large numbers of pathfinding agents I saw the bulk of the cpu workload being spent in calls to the Physics engine by pathfinding agents. I haven’t returned to narrow it down, but when I get a chance I’ll do my best to isolate the call that is eating up the most resources (if I remember correctly it was difficult to identify which Physics call was the culprit even with deep profiling turned on.

Did you get a chance to read the post by the Unity physics dev? It sounds to me like he’s clearly making the case for not needing to call SyncTransforms, especially if you’re already moving a GameObject using an attached Rigidbody/Rigidbody2D (in the case of AIPath you are, right?). And again, I was able to comment out these calls and not have any problems in my own testing, which includes procedurally building graphs. Probably worth mentioning that in my workflow when I procedurally build rooms for agents to move around in, I explicitly call SyncTransforms to ensure the graphs in those rooms are built and scanned correctly.