The next few articles are going to cover how I implemented server-authoritative netcode for FPS games in Unity. The theory and ideas I'm going to discuss will apply to any language, engine, or game. Here's a few things to keep in mind:
If you want to play games with other people across a network - like online or at a LAN party - you need some way to facilitate connection and communication between different computers or "clients". In games, the stack that handles this connection, communication, and synchronization is called "netcode" (and probably a million other terms).
There are a few different ways of structuring your netcode, each with benefits and drawbacks depending on what kind of game you are making.
To start, you need to choose how you want clients/computers to communicate: Directly to each other (Peer to Peer or P2P), or between each client and a server (client-server).
For the type of games I make, client-server architecture is the way to go. We want one central source of truth who controls the game and handles sorting out the game and keeping everyone in sync. For the small, first-person shooters I make, I elect one player the "host" who runs the server and plays as a client*. This means anyone can host a server and play - no dedicated servers (though they are possible).
*You could get into semantics about P2P and player-hosted servers but I won't."We want one central source of truth". Cheaters are no fun in games, and we'd like to prevent cheating where possible. To make this easier, the server has "authority", meaning it controls the state of all objects in the game. Why does this matter? Let's look at client vs server authority.
The client has control over certain objects in the game - typically their player. They move/shoot/interact and tell the server what they did.
Obviously this is exaggerated and the server can try and run some "sanity checks" to prevent obvious cheats, but it's much easier to cheat when the client has authority. Look at a game like Rust - the client has authority over their player, and some cheats allow you to climb up walls even while the server checks on its end.
Apart from that negative, it feels really good when you have authority over your player. Your inputs apply instantly and you never feel "cheated" out of an action.
The clients just tell the server what they'd like to do. They tell the server what buttons they are pressing and the server tells them what happened.
Many cheats are very difficult or impossible when the server has authority over everything. Flyhacking, speed cheats, shooting through walls, etc. are gone (aimbots and wallhacking are still issues). However, there is one problem: the network.
I tell the server I am pressing W. It takes 100ms for my packet to get there. The server moves me and tells me where I went. Another 100ms for that packet to get to me. I press W and only see myself move 200ms later. Terrible! How can we get around this?
These are the methods used to deal with network conditions and make server-authoritative games feel good to play. They are discussed in the next couple of articles.
Here are some great articles I used to research and implement my netcode: