Client-Side Prediction With Physics In Unity


I made a demo showing how to do client-side prediction with physics-based player movement in Unity - GitHub.



Back in early 2012 I wrote a blog post about kind-of-but-not-really implementing client-side prediction of physics-based player movement in Unity. That nasty workaround that I described, is no longer necessary thanks to Physics.Simulate(). That old post is still one of the most popular posts on here, but with Unity today, that information is just sort of wrong. So here goes, the 2018 edition.

Read More

Integrating Mono.Cecil with Unity

Have you seen JB Evain's talk at Unite? If you haven't, then you probably can't, as it has been inexplicably taken down. The subject matter is mostly about using a library called Mono.Cecil to actually alter a .NET assembly, allowing for both injecting and removing code. This gives us the ability to create C# post-processors which operate on the compiled assembly, not the code files. I've been writing some pretty repetitive code of late, and so I figured I'd try using this approach to simplify things. There is a problem with this though - there isn't an "on assembly built" event in Unity that we can hook in to.


  1. Compile our code to a managed DLL, and set up Visual Studio to run the assembly post processor on it before placing it in the output directory.
  2. Hack it up.

One of the things I really hate is a shitty workflow, and (at least at the moment) I find using managed DLLs in Unity to be conducive to exactly that. For example, if I double click on a message in the console, it won't open the file in my IDE like it does when I'm letting Unity compile everything for me. In addition, I'd like to have the option to distribute what I'm working on as an Asset Store editor extension, and imposing upon users that they must compile their code to a managed DLL and import it into their project is pretty much out of the question if you ask me. So, I opted for option #2.

Read More

Fun With Bitcoin

After I haven't posted anything for what seems like forever, this will be just a short one. I've been interested in Bitcoin and cryptocurrencies for a couple of years now, and I've played around with the API recently, and decided to make a visualiser in the same vein as BitListen and so on. New transactions create an explosion of fireworks, the bigger the amount transferred - the bigger the explosion.

See it here

Source Code on Github

Client-side Prediction in Unity

If you're making a multiplayer game in Unity and your networking model includes a fully authoritative server, you might have found movement to be a bit of a stumbling block. Client-side prediction is how lag tends to be hidden, Glenn Fiedler has an awesome series of articles, this one explains client-side prediction nicely.

To summarise - clients send their input (e.g. key presses) to the server which computes their updated position in the game world, and sends it back to them. To hide the lag, the client runs the movement code locally, and records its input and position each frame. When an update is received from the server, it looks through all the recorded positions and compares it with the data received from the server. If the two are out of sync by too large a margin, it retrospectively corrects the client by moving them to the correct position, and then runs through all the stored inputs, replaying the users actions up to the present time. This "rewind and replay" system is fine under certain circumstances, but a big problem in Unity is physics.

For about two years I've been developing a Unity multiplayer FPS on and off. Naturally, players can run about, jump up and down, collide with objects, and so on. I have no way of triggering the Rigidbody component on the player to simulate a frame, so I can't rewind and replay can I? Well I can, if I roll my own basic Rigidbody class:

Read More