Physics.Simulate Bug?

Hi, so I’m working on a multiplayer game that involves, in the code, manually stepping the physics engine using physics.simulate() a bunch of times in order to keep the client in-sync with the server.

For some reason this is causing extreme camera jitter/stutter. If I remove physics.simulate from my code, the jitter is gone. Both nvidia overlay and the unity profiler say that I’m getting 100fps in a build, yet the stutter persists. I had a friend test the build as well, and he noticed the stutter too.

Here’s a view into the profile of an actual build of the game. As you can see, it’s operating at a steady 100fps, so there’s really no reason for it to be stuttering like this at all. Unfortunately I can’t record the stuttering, because my software will not pick it up, but it’s noticeable to me, and to other people.

(Those are just tiny 3 byte allocations, this isn’t a garbage collection issue)

And yes this doesn’t seem to be an issue in my code either, since the stutter persists inside of the actual scene view in the editor. If I remove physics.simulate(), it’s smooth as butter again.

I’m on 2020.3.11f

Stuttering is not always an issue of low framerate. It can simply be caused by unnatural or jumpy movement caused by your code.

Perhaps your code is “violently” correcting your object positions back to server positions.Or simulating physics one too many or one too few times, resulting in jittery, unnatural movement. I’m throwing out random guesses here, as I have no idea how your code works. but I’m just pointing out that you have to look beyond just framerate drops as sources of “stuttery” looking movement.

You need to look no further than what happens when you have physics objects moving around the scene without interpolation enabled to see an example.

Unfortunately I wish you were right, but it’s not my code (as far as I can see).

My local player is predicting himself properly and there’s literally no corrections being done. It seems as if calling physics.simulate(0.02f); a bunch of times in a row on the same frame does something to the camera, or the transforms of every object in the scene, and makes them jitter horribly.

When I turn off the physics.simulate(0.02f); loop, the jittering goes away, and everything is completely smooth again.

Yes it does something - it moves all the objects that have dynamic Rigidbodies. Is the camera being moved to adjust for this?

I know how it works.
Let me explain briefly what I’m doing.

Every 20ms or so, the server sends data to each client regarding their own car’s position/rotation, velocity, angular velocity etc. Each client predicts itself ahead of the server, so when the packet comes in, each client resets their position, rotation, velocity, angular velocity etc to the server’s value, and then predicts however many ticks he needs to, so that he’s caught up again to his local time. 1 tick = 1 physics.simulate() event.

I’ve already looked at this 100 times over, and the client is not mis-predicting himself in any way. The car always is where it needs to be after every correction, except while this is occurring, there’s always visible jitter. Let me reiterate, that there is no mis-prediction going on, but physics.simulate is causing something to jitter.

if I turn off this loop however, so that physics.simulate() doesn’t run in this fashion, only runs during the MAIN simulation, it’s 100% buttery smooth.

I’m going to run some more tests, but hopefully someone has some suggestions on what to do.

Is this the problem perhaps? That interpolation breaks when doing manual physics sim?

https://discussions.unity.com/t/742927

1 Like

Hmm that’s an interesting post, and it might actually tie into what I’m doing.

I’m not using unity’s built-in interpolation for rigidbodies either, I have my own fixedupdate loop and I manually calculate the interpolation ratio. But it might be sort of a global problem. Who knows. I’ll have a look