I have a project here where I implemented the above suggestion of
However, it is producing some odd results for me.
I made this project modeled after how Rival has their online FPS example set up. How they handle inputs is like this:
-
OnlineCharacterCommandsSystem.cs
(only runs on Client)
Takes inputs on Client and puts them into a ICommand
struct and uses AddCommand
to put it into a buffer. This is where I do the incrementing of “you pressed jump” into a variable called byte JumpRequested
.
-
Ghost Authoring Component has “Support Auto Command Target” checked in Inspector, so Commands are sent to Server.
-
OnlinePlayerControlSystem.cs
(runs on both Client and Server)
Reads the ICommandBuffer
, gets the ICommand
buffer at a tick and a previous tick, and performs the subtraction (as suggested). If the tick - prevTick > 0
, then sets the bool
value of Jump to true.
-
OnlineCharacterSystem.cs
(runs on both Client and Server)
Loops through all Entities that have the OnlineCharacterInputs
struct. If any of those have a Jump value set to true, then the character (a cube) moves up 1 meter on the y-axis.
However, when I make a build of the project on my machine, I get many inconsistent instances where the Server never seems to receive the command. Here is a set of simple Debug.Log statements I have.
The first block here, is what the Debug.Logs look like on a failure.
Client: Jump Pressed 1 times at frame: 398. Added to buffer at tick 152
Client Jumped. Tick: 152 Frame:398
Client: Character Jumped. Frame:398
What ends up happening is that the Client makes a very short attempt to jump, but then the Server corrects it and a jump never occurs.
Essentially what it looks like to me, is that the Server never receives an ICommand
at tick 152 that has JumpRequested
with a value of 1. Instead it receives one that has JumpRequested
with a value of 0.
And here is what the Debug.Logs look like on a success.
Client: Jump Pressed 1 times at frame: 993. Added to buffer at tick 476
Client Jumped. Tick: 476 Frame:993
Client: Character Jumped. Frame:993
Client Jumped. Tick: 476 Frame:994
Client Jumped. Tick: 476 Frame:995
Client Jumped. Tick: 476 Frame:997
Server Jumped. Tick: 476 Frame:998
Server: Character Jumped. Frame:998
This causes a successful jump, however I’m not a big fan of how Jump ends up being true for 5 frames. But this is not yet a problem for me that I want to address.
I would really, really appreciate it if someone could take a look at this project and see if this is a problem with my implementation, is a Unity bug, or if it even happens on your machine. I am attaching a zip of the project files.
This uses Unity 2020.3.34f1.
To build it, go to the BuildConfig
folder, highlight OnlineClientServerBuildConfig
, press Build
in the top-right of the Inspector.
All you need to do to run the build is press the Host button on the main menu screen. This will create a server and connect to it as a client.
In order to see the Debug.Logs, I find the best way is to go into Powershell and paste this in there after running the build.
Get-Content "$($env:LOCALAPPDATA)\..\LocalLow\tylo\NetCodeProblem_ClientServer\Player.log" -Wait
8580337–1149499–NetCodeProblem.zip (78.7 KB)