I am in the same discord server as someone named fholm, who's full time job is doing networked physics for a company called Exit Games. He showed off a pretty cool demo in the discord, similar to my demo, which inspired me to make this. I asked him if I could recreate his demo, and he said that it would be a good exercise in learning networking, and said sure.

What it does

The client connects to the server, and controls the ball on the server by sending input packets. You are able to see the ball moving in real time on the client because of client side prediction, which is where you predict where the ball will go and move it there before you get a message from the server saying where it actually went.

How I built it

I started out with an empty unity project, and built a simple script that allowed me to move a sphere around. Then I installed LiteNetLib and started experimenting with it. I made a basic setup where the client just sends the server the ball's position. After I figured that out, I spent most of my time building a tick system and then figuring out how to sync the game state and send inputs.

Challenges I ran into

For a while, I kept getting a lot of NullReferenceExceptions, which are extremely annoying. They were usually caused by me making a field instead of a property, or forgetting to assign a variable. Also, at first, when I sent input from the client to the server, the ball would go extremely fast in the direction I had pressed, causing the server to become super out of sync with the client. I was eventually able to solve this by saving states and reloading them.

Accomplishments that I'm proud of

I made a demo with authoritative physics. This is something I've wanted to do for a while but I haven't gotten to, so it's super awesome that I got to make this.

What I learned

LiteNetLib is an amazing library, networking isn't as hard as it seems, and hackathons are a lot of fun.

What's next for Authoritative Ball Physics

I plan on cleaning up the code a bit and patching any bugs that I haven't noticed, and then making my own game using some of the code that I made here.

I also want to make it so that multiple clients can join one host. This should (hopefully) be as easy as changing 2 lists from just storing game ticks to storing clients AND game ticks.

Built With

Share this project: