I’m using Unity for robotics simulation, and would like to have Unity update its physics at a fixed real time rate. I know that fixedUpdate()
is called at a fixed rate according to game time, but in reality it’s frequency varies wildly with framerate. I believe I understand why this is fine for games, but if I want to receive a real time sensor stream from Unity this causes issues.
As an example, when I set fixed timestep to 0.001 and record the delta time in real life between each fixed update, I get this

What I’d like to see is a tighter grouping of delta time around 0.001 like

Is there a way to achieve this with the default Unity physics engine PhysX? I should say that I did not record these values directly from Unity, but through a websocket connection using ROS#.
This issue has been brought up and described in better detail here. They do bring up the possible solution of setting Physics.autoSimulation = false
and running Physics.Simulate()
manually, but I have not managed to get this to work.
Any advice in solving this would be appreciated. If necessary, I’m okay with using a different physics engine.
Well okay short answer: What you are looking for is an actual “real-time” simulation. Unity can not do this as far as i am aware.
To dive a bit further in: 0.001seconds is a really really small stepsize. I doubt that values this small are encouraged by Unity. You could perhaps get a bit further in this direction if you scrap unitys default update cycle and create your own from scratch. There are functions to change the updatecycle but i have never reall dug into this. (you can for example remove Update and exchange that by something else. This way you could remove unnecessary steps)
Either way the result you show makes sense and your desired behaviour will never be reached with default behaviour.
Unity will not trigger Updates/Fixed Updates “early” just because time is close. It will always trigger on time or later. Then again i guess you can assume a somewhat “fixed deviation time”. This time normally is negligable but somewhat constant no matter how large the fixed step size. However if you stepsize itself is that small you will have issues as the deviation suddenly could be as large as your step itself. And in the end i doubt that you’ll be able to change anything about this.
So to sum it up: yes you could perhaps get your desired deviation distribution by manually simulating an thus enableing “early” update execution. However i highly doubt you’ll be able to get the deviation magnitude you desire with such a small step size. That beeing said you perhaps should check out other physics simulations (perhaps some that are actually made for real time computation though that sometimes can require extra hard and software since neither Standard Linux or Standard Windows is good at real time computations) This can also include to check if unitys efforts to include the havoc engine has been fruitful yet. (Last time i checked you needed a license though)
Hope this made sense and was helpful. Good luck to you.