Networked Physics in Unity

I recently dove into this topic for a class project where we were using Unity for console development, specifically the Nintendo Switch. I thought it would be a cute idea to make a game where two capsules would joust each other off of a platform by dashing quickly at each other. And wouldn’t it be amazing if it supported network multiplayer! Fortnite just got their crossplay working on the Switch, I can do that too between Switch and PC!

This was a series of uninformed, bad ideas.

Design Notes

If you’re going to make a networked based on physics game don’t design in high velocity impacts as part of the core gameplay loop. The best networked physics games I’ve played (Gang Beasts / Humans Fall Flat) already knew that so most of the interactions between players are slow in comparison to normal fighting games. They also made their games third person, guess who didn’t do that? Since I didn’t figure that out until a week and a half into this three week project, I just did my best. In the end there’s still a lot of cases where players tunnel through or embed their first person cameras into each other, but through clever implementation I was able to minimize tunneling and the amount of visible rubberbanding.

Implementation Details

Most of the implementation details can be found in this article which was inspired by this article. Thanks to both Joe Best-Rotheray and Glenn Fiedler for those amazing resources.

The only thing I had to figure out was how to make Best-Rotheray’s model work for a scene where two players had control over two different objects. The main issue being that if a player’s predictive simulation caused a change in velocity for the other player’s object we need to account for that when we receive position information for the other players object. So the solution I came up with was to store the other player’s object’s velocity every frame so that when authoritative positional information is received I can rollback to the given frame and then replay all velocities. While this does introduce simulation divergence due to the object’s position changing what the velocity of the object would have been, it did the trick for this project.

One last note, I think Best-Rotheray’s allowed divergence (0.0003f) is too strict (especially when trying to do console cross play) so I bumped mine down to 0.001f allowed positional divergence.

Things I Learned

Networking physics is hard, but as with most things it can be made a lot easier with clever design decisions. Interpolation will hide your sins. Manage your scope better for simple class projects.

Don’t design a game like I did, but do take a look at the resources I’ve linked if you want to network your Unity physics.