top of page
  • Writer's pictureNate Otis

Lag Compensation Pt 1

Updated: Aug 3, 2023

Howdy!


Welcome back to my blog series on networking in games. Last week we were able to get a pretty good solution working for simulating other player's movement so now both our player and other players' positions will be simulated correctly to all clients. But what if we want to do something with that other player? As we discussed last week, we always see other players one network update behind their actual position on the server due to how we are simulating their movement. This works great for simulating movement but it starts to break when we try to interact with that player because we are interacting with where they were. This discrepancy is usually small but can matter a lot in situations where spatial accuracy is important, like in first-person shooters. Luckily there is a solution known as lag compensation that addresses this issue. We'll be working on setting up the foundation for that solution this week.


Giving our player a gun


In order to keep things simple for now, our gun is simply going to be a hitscan weapon that performs a single raycast on-click in the direction that the player is looking. I decided to add a MW2-esque hitmaker to really give that true FPS feel (and also to show that we are actually shooting/hitting a target since our "gun" isn't really visible).


[[Image of raytrace from player to object]]


Networking the gun


If we want this shot to actually affect other players though, we instead need to request to shoot on the server not our local client, and perform hit detection there. Thus, when the player presses the fire button we should instead call a Server RPC that performs the hit detection: [[Code snippet of hit detection code Server RPC]]


As you can see above, no matter what happened with the shot we sent a response to the instigating client letting them know what should happen and if they did successfully hit another player, we additionally sent an RPC to that player letting them know that they were hit (for which I implemented a simple red screen effect to show when the other client recognizes they were hit)


[[Video of shooting other players networked]]


The Problem


And that seems to work fine ... until our players start moving that is

In the video above, the blue collider represents where the instigating client (the player that took the shot) saw the other player at the time of shooting. And the red collider shows where the other player actually was on the server when the shot was processed. As you can see, there is quite a difference between these positions, certainly enough for players to notice when their shots should've hit.


The Solution


As I mentioned above, this problem is a result of the fact that each client is simulating the other clients one network update behind so where they are shooting at will always be significantly behind where the other player's actually were. We can mitigate this problem by leveraging the fact that the server knows about all player movements to save a list of each player's recent movements. Using this, we roll back each player's position to the point they were at when the instigating client actually clicked shoot and evaluate the collisions there. This is known as Lag Compensation and will be what I will be working on and discussing in next week's blog post. See you then! Githhub: [[add Github link]]

4 views0 comments

Recent Posts

See All

コメント


bottom of page